我已经在UITapGestureRecognizer
中添加了简单的UIImageView
并创建了手势方法,当用户点击/触摸 UIImageView
时会调用该方法
我的代码是:
UITapGestureRecognizer *eagleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eagleTapGestureIsCall:)];
eagleTapGesture.numberOfTapsRequired = 1;
[self.egaleImgView addGestureRecognizer:eagleTapGesture];
手势方法:
- (void)eagleTapGestureIsCall:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
NSLog(@"Eagle is Tapped X - %f",location.x);
NSLog(@"Eagle is Tapped Y - %f",location.y);
}
在这里,如果我在
UIView
而不是self.egaleImgView
上添加Gesture,则它可以工作(我的意思是Gesture方法被调用),但是为什么它在self.egaleImgView
上不起作用?在谷歌搜索之后,我发现
UIView
可能在触及UIImageView
之前就拦截了触摸。所以我在
viewDidLoad
的开头添加了以下行self.view.userInteractionEnabled = NO; /// Here i set userInteractionEnabled = NO;
以下描述结构以更好地理解。
main UIView
| |
(Sub view) mainBackGroungImageView //// size is similar to size of main UIView
| |
(Sub view) gestureAdded ImageView
编辑:
抱歉,我忘了提到我已经在我的imageView中添加了
self.egaleImgView.userInteractionEnabled = YES;
,但对我来说不起作用。请给我有关我的问题的建议。
提前致谢。
最佳答案
设置您的ImageView.userInteractionEnabled = YES;
,因为默认情况下为否。并且最好将视图的用户交互设置为“是”。
关于iphone - 为什么UITapGestureRecognizer对于UIImageView不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17208162/