我正在制作一个应用程序,其中有一个背景 View ,并且有六个 UIImageView
作为 subview 。我有一个 UITapGestureRecognizer
来查看何时点击其中一个 UIImageView
并且下面的 handleTap 方法是手势识别器调用的方法。但是,当我运行它时,hitTest:withEvent:
总是返回背景 View ,即使我点击其中一个 imageViews 也是如此。和调用hitTest的事件有关系吗?
谢谢
- (void) handleTap: (UITapGestureRecognizer *) sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView: sender.view];
UIView * viewHit = [sender.view hitTest:location withEvent:NULL];
NSLog(@"%@", [viewHit class]);
if (viewHit == sender.view) {}
else if ([viewHit isKindOfClass:[UIImageView class]])
{
[self imageViewTapped: viewHit];
NSLog(@"ImageViewTapped!");
}
}
}
最佳答案
默认情况下,UIImageView 配置为不注册用户交互。
来自 UIImageView 文档:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
因此,在初始化 View 之后,您应该:
view.userInteractionEnabled = YES;
这将重新打开交互,您应该能够注册触摸事件。
关于iphone - HitTest :withEvent: Not Working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271409/