![](/img/trans.png)
[英]Not able to register a tap on the screen using tapGestureRecognizer
[英]iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view
在我的应用程序中,我有一个图像和一个UITextView
。
我已经为两个视图创建了一个UITapGestureRecognizer
,但是问题是,无论我在屏幕上单击什么位置,都只会执行与UITextView
关联的方法。
即使单击图像,也只会执行与UITextView
关联的UITapGestureRecognizer
方法。
以下是我实现的代码:
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
[[self view] addGestureRecognizer:tapGestureRecognizer];
//The following are the methods associated
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(@"I am in handleTapFrom method");
}
- (void) handleTapFromImage: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(@"I am in handleTapFrom Image method");
[self.view makeToast:@"Your verification code does not match. Re-enter your verification code"];
}
我确定我在这里错过了一些东西。
据我所知,情节提要中的关联是正确的。
请纠正我我要去哪里了
谢谢你的时间
您不应该在self.view上添加手势。
它应该添加到您要为其识别点击事件的视图上。
您正在[self view]
对象上设置了两个Tap Gesture Objects。
另外,让它imageObj
的UIImageView
对象应该具有userInteractionEnabled = YES
。
代替:
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
你应该做:
[imageObj setUserInteractionEnabled:YES];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
通常,在要使用手势对象的对象上使用-addGestureRecognizer:
假设您有一个名为myTapGesture
的UITapGestureRecognizer
对象。
然后,使其工作...
UILabel *lblSomeObj
,它将是:
[lblSomeObj addGestureRecognizer:myTapGesture];
UIView *vwSomeObj
,它将是:
[vwSomeObj addGestureRecognizer:myTapGesture];
您需要包括以下代码:
[tapGestureRecognizerImage requireGestureRecognizerToFail:tapGestureRecognizer];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
只需将手势添加到各个视图中,而不要添加到self.view中。
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
在UIImageView对象上添加手势,并确保将图像视图userInteractionEnabled
设置为YES
imageObj.userInteractionEnabled = YES;
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.