Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gesture recognizer and button actions

I have a view hierarchy that looks something like this:

UIView (A) UIView > UIImageView UIView > UIView (B) UIView > UIView (B) > Rounded Rect Button UIView > UIView (B) > UIImageView UIView > UIView (B) > UILabel 

I've attached gesture recognizer(s) to my UIView (B). The problem that i'm facing is that i don't get any actions for the Rounded Rect Button which is inside the UIView (B). The singleTap gesture recognizer captures/overrides the button's Touch Up Inside event.

How can i make it work? I thought that the responder chain hierarchy will make sure that the button touch event will be given preference, and it WILL get triggered! What am i missing?

Here's some related code:

#pragma mark - #pragma mark View lifecycle (Gesture recognizer setup)  - (void)viewDidLoad {     [super viewDidLoad];      // double tap gesture recognizer     UITapGestureRecognizer *dtapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognizer:)];     dtapGestureRecognize.delegate = self;     dtapGestureRecognize.numberOfTapsRequired = 2;     [self.viewB addGestureRecognizer:dtapGestureRecognize];      // single tap gesture recognizer     UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureRecognizer:)];     tapGestureRecognize.delegate = self;     tapGestureRecognize.numberOfTapsRequired = 1;     [tapGestureRecognize requireGestureRecognizerToFail:dtapGestureRecognize];     [self.viewB addGestureRecognizer:tapGestureRecognize];      // add gesture recodgnizer to the grid view to start the edit mode     UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];     pahGestureRecognizer.delegate = self;     pahGestureRecognizer.minimumPressDuration = 0.5;     [self.viewB addGestureRecognizer:pahGestureRecognizer];      [dtapGestureRecognize release];     [tapGestureRecognize release];     [pahGestureRecognizer release]; }  #pragma mark - #pragma mark Button actions  - (IBAction)buttonTouchUpInside:(id)sender {     NSLog(@"%s, %@", __FUNCTION__, sender); }  #pragma mark - #pragma mark Gesture recognizer actions   - (void)singleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {     NSLog(@"%s", __FUNCTION__); }  - (void)doubleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {     NSLog(@"%s", __FUNCTION__); }  - (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {      switch (gestureRecognizer.state) {          case UIGestureRecognizerStateEnded: {             NSLog(@"%s", __FUNCTION__);              break;         }         default:             break;     } } 
like image 672
Mustafa Avatar asked Jan 28 '11 05:01

Mustafa


Video Answer


1 Answers

In the "shouldReceiveTouch" method you should add a condition that will return NO if the touch is in the button.

This is from apple SimpleGestureRecognizers example.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {      // Disallow recognition of tap gestures in the segmented control.     if ((touch.view == yourButton)) {//change it to your condition         return NO;     }     return YES; } 

hope it will help

Edit

As Daniel noted you must conform to UIGestureRecognizerDelegate for it to work.

shani

like image 156
shannoga Avatar answered Oct 16 '22 07:10

shannoga