Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize swipe in all 4 directions?

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?

like image 798
elp Avatar asked Nov 18 '11 11:11

elp


People also ask

How do you use swipe gestures in Swift 4?

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(_:)

What is Pangesture?

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.


2 Answers

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.


EDIT

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.

like image 59
jbat100 Avatar answered Oct 03 '22 18:10

jbat100


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 UISwipeGestureRecognizers for that purpose, see my answer here: https://stackoverflow.com/a/16810160/936957

like image 20
Yunus Nedim Mehel Avatar answered Oct 03 '22 18:10

Yunus Nedim Mehel