我已经在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/

10-14 21:20
查看更多