简体   繁体   English

iOS-TapGestureRecognizer-Tap适用于整个屏幕而不适用于视图

[英]iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view

In my app, I have an image and a UITextView . 在我的应用程序中,我有一个图像和一个UITextView
I have created a UITapGestureRecognizer for both the views but the issue is that wherever I click on the screen, only the method associated with the UITextView gets executed. 我已经为两个视图创建了一个UITapGestureRecognizer ,但是问题是,无论我在屏幕上单击什么位置,都只会执行与UITextView关联的方法。
Even if I click on the image, only the UITapGestureRecognizer method associated with the UITextView gets executed. 即使单击图像,也只会执行与UITextView关联的UITapGestureRecognizer方法。

Following is the code I've implemented: 以下是我实现的代码:

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"];
}

I am sure I am missing something here. 我确定我在这里错过了一些东西。
The association in storyboard is correct to my knowledge. 据我所知,情节提要中的关联是正确的。

Please correct me where I am going wrong 请纠正我我要去哪里了

Thanks for your time 谢谢你的时间

You should not add gesture on self.view. 您不应该在self.view上添加手势。

It should get added on the view for which you want to identify tap event. 它应该添加到您要为其识别点击事件的视图上。

You are setting both the Tap Gesture Objects on [self view] object. 您正在[self view]对象上设置了两个Tap Gesture Objects。
Also, the UIImageView object, lets call it imageObj , should have userInteractionEnabled = YES . 另外,让它imageObjUIImageView对象应该具有userInteractionEnabled = YES

instead of: 代替:

[[self view] addGestureRecognizer:tapGestureRecognizerImage];

you should do: 你应该做:

[imageObj setUserInteractionEnabled:YES];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];

You generally use -addGestureRecognizer: on the object you want your gesture object to work on. 通常,在要使用手势对象的对象上使用-addGestureRecognizer:
Say you have a UITapGestureRecognizer object called myTapGesture . 假设您有一个名为myTapGestureUITapGestureRecognizer对象。

Then, to make it work... 然后,使其工作...

  1. on a UILabel *lblSomeObj it will be: UILabel *lblSomeObj ,它将是:
    • [lblSomeObj addGestureRecognizer:myTapGesture];
  2. on a UIView *vwSomeObj it will be: UIView *vwSomeObj ,它将是:
    • [vwSomeObj addGestureRecognizer:myTapGesture];
  3. etc... 等等...

You need to include this piece of code:- 您需要包括以下代码:

 [tapGestureRecognizerImage requireGestureRecognizerToFail:tapGestureRecognizer]; 
[imageObj addGestureRecognizer:tapGestureRecognizerImage];

Just add the gesture in the respective views and not in self.view. 只需将手势添加到各个视图中,而不要添加到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];

Add gesture on UIImageView object and make sure that image view userInteractionEnabled is set to YES 在UIImageView对象上添加手势,并确保将图像视图userInteractionEnabled设置为YES

imageObj.userInteractionEnabled = YES;
[imageObj addGestureRecognizer:tapGestureRecognizerImage];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM