Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling gesture recognizers in iOS6

Apparently iOS 6 tries to automatically handle the situation when you have a gesture recognizer and a UIButton in the same place, being activated for the same gesture.

This new automatic handling solves the issue when you want to click the button instead of activating the gesture recognizer, but creates a new problem when you want the gesture recognizer to act.

In iOS 5 you could implement:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

to transfer the action to the UIButton when in a conflict.

This doesn't seem to work in iOS 6. Also, inverting the behavior of this method (because now the UIButton has the priority instead of the gesture recognizer) won't work.


Complete method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UIControl class]]){
        return NO;
    }
    return YES;
}
like image 691
jturolla Avatar asked Sep 20 '12 18:09

jturolla


1 Answers

I've done this to workaround the issue, change it as you see fit:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
      [self MyCommonSelector];// this will work for ios 5
      return Yes;
 }

Add target in button where you declare so this will call in iOS 6:

[self.myButton addTarget:self action:@selector(MyCommonSelector)
  forControlEvents:UIControlEventTouchUpInside];

Do your stuff in this method which will also be called on button tap and from the gesture you need to call:

-(void)MyCommonSelector
 {
    //Do your stuff here what you want to do with Gesture too.
 }
like image 65
Nikunj Avatar answered Oct 12 '22 02:10

Nikunj