如何向对象添加事件识别器?
我有以下代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches happened");
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.mainView];
for (UIView *view in self.mainView.subviews)
{
if ([view isKindOfClass:[ViewController class]] &&
CGRectContainsPoint(view.frame, touchLocation))
{
NSLog(@"my Touch Happened");
UIImageView *tmp = (UIImageView *)view;
if (selectedItem != tmp)
[self setSelected:tmp];
}
}
}
我的目标是,如果我的图像被触摸,不是轻敲或长按而是完全被触摸,那么从 NSLog(@"found whats being touched");
我以前使用过 UIGestureRecognizers,你可以将它们添加到任何你想要的地方。
[self.SomeImage addGestureRecognizer:rotate];
但是我如何告诉我的对象听这段代码呢?
我想知道触摸区域是否与另一个对象所在的区域相同?也就是说,如果用户试图平移一个项目。我可以处理平底锅。但是锅前的第二个是触摸。我该如何找到那个触摸以及与之关联的对象!
最佳答案
UIViewController和UIView直接支持- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
您可以直接将您的函数放入此类对象中。 (即 UIView 或 UIViewController)
关于ios - iOS 中的 TouchBegan 和响应者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22572586/