I need to recognize swipes in all directions (Up/Down/Left/Right). Not simultaneously, but I need to recognize them.
I tried:
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; Swipe.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp); [self.view addGestureRecognizer:Swipe]; [Swipe release];
but nothing appeared on SwipeRecognizer
Here's the code for SwipeRecognizer:
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender { if ( sender.direction == UISwipeGestureRecognizerDirectionLeft ) NSLog(@" *** SWIPE LEFT ***"); if ( sender.direction == UISwipeGestureRecognizerDirectionRight ) NSLog(@" *** SWIPE RIGHT ***"); if ( sender.direction == UISwipeGestureRecognizerDirectionDown ) NSLog(@" *** SWIPE DOWN ***"); if ( sender.direction == UISwipeGestureRecognizerDirectionUp ) NSLog(@" *** SWIPE UP ***"); }
How can I do this? How can assign to my Swipe object all different directions?
A swipe gesture recognizer detects swipes in one of four directions, up , down , left , and right . We set the direction property of the swipe gesture recognizer to down . If the user swipes from the top of the blue view to the bottom of the blue view, the swipe gesture recognizer invokes the didSwipe(_:)
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.
You set the direction like this
UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; Swipe.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp);
That's what the direction will be when you get the callback, so it is normal that all your tests fails. If you had
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender { if ( sender.direction | UISwipeGestureRecognizerDirectionLeft ) NSLog(@" *** SWIPE LEFT ***"); if ( sender.direction | UISwipeGestureRecognizerDirectionRight ) NSLog(@" *** SWIPE RIGHT ***"); if ( sender.direction | UISwipeGestureRecognizerDirectionDown ) NSLog(@" *** SWIPE DOWN ***"); if ( sender.direction | UISwipeGestureRecognizerDirectionUp ) NSLog(@" *** SWIPE UP ***"); }
The tests would succeed (but the would all succeed so you wouldn't get any information out of them). If you want to distinguish between swipes in different directions you will need separate gesture recognizers.
As pointed out in the comments, see this answer. Apparently even this doesn't work. You should create swipe with only one direction to make your life easier.
Unfortunately you cannot use direction
property for listening the recognizer; it only gives you the detected directions by the recognizer. I have used two different UISwipeGestureRecognizer
s for that purpose, see my answer here: https://stackoverflow.com/a/16810160/936957
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