Question

In my app, I have an image and a 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.
Even if I click on the image, only the UITapGestureRecognizer method associated with the UITextView gets executed.

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

Was it helpful?

Solution 2

You are setting both the Tap Gesture Objects on [self view] object.
Also, the UIImageView object, lets call it imageObj, should have 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.
Say you have a UITapGestureRecognizer object called myTapGesture.

Then, to make it work...

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

OTHER TIPS

You should not add gesture on self.view.

It should get added on the view for which you want to identify tap event.

Just add the gesture in the respective views and not in 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

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

You need to include this piece of code:-

 [tapGestureRecognizerImage requireGestureRecognizerToFail:tapGestureRecognizer]; 
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top