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
You should not add gesture on 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.
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...
UILabel *lblSomeObj
it will be:
[lblSomeObj addGestureRecognizer:myTapGesture];
UIView *vwSomeObj
it will be:
[vwSomeObj addGestureRecognizer:myTapGesture];
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.
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.