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!
Add four swipe gesture recognizers to your view. Set each one with the target direction from the attribute inspector. You can select right, left, up or down. One by one, select the swipe gesture recognizer, control + drag to your view controller.
A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.
There are seven type of gesture that support in ios .
A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With