Simultaneous gesture recognizers in Iphone SDK
I need to catch two different swipping gestures using UISwipeGestureRecognizer
(for example, UISwipeGestureRecognizerDirectionRight
and UISwipeGestureRecognizerDirectionLeft
). When I add two different recognisers with addGestureRecognizer method, only last added recognizer works. I've read that I need to implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method of UIGestureRecognizerDelegate
protocol, but nothing works.
Can anyone help with simple example of catching two or more same gestures? Thanks!
Solution 1:
It was really easy:
At first we should create class, that implements UIGestureRecognizerDelegate
protocol:
@interface MyGestureDelegate : NSObject <UIGestureRecognizerDelegate>
@implementation MyGestureDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
And use it like this:
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
[self.view addGestureRecognizer:swipeGestureLeft];
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[swipeGestureLeft release];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeGesture];
MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init];
[swipeGesture setDelegate:deleg];
[swipeGesture release];
Solution 2:
The answer: "Um, a quick look at the documentation..." from Phoenix absolutely will not work!
He is setting a mask, then using the same variable to test as if the recognizer cleared it and set a single bit. It stores, as he correctly quoted from the documentation:
The permitted directions of the swipe
sender.direction
will simply return the mask you set initially and in his example, will never resolve to a single direction!
UISwipeGestureRecognizerDirectionRight == 1
UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft == 3
Additionaly, for most cases you don't need to:
- setup a delegate
- permit simultaneous gesture recognition (unless you want simultaneous swipes; not likely)
- send the recognizer to the selector
The following works for me:
UISwipeGestureRecognizer* swipe;
swipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeL)] autorelease];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:swipe];
swipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeR)] autorelease];
swipe.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:swipe];