how detect swipe gesture direction?

27,457

Solution 1

The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.

The easiest would be to use two separate gesture recognizers instead. You could also inspect the location of the touch when the gesture starts and when it ends with the locationInView: method.

Solution 2

Here is an example from one of my projects:

    // ...

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(didSwipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(didSwipe:)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUp];

    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeDown];

    // ...

- (void)didSwipe:(UISwipeGestureRecognizer*)swipe{

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Swipe Left");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Swipe Right");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"Swipe Up");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"Swipe Down");
    }
}

Solution 3

Extending omz's solution:

self.myView is the view I want to put the gesture recognizer on. The code below is not tested, I guess it would be better to keep the recognizers as propertys and add them inside viewDidLoad() or xib file. self is a UIViewController.

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight ];
[self.view addGestureRecognizer:swipeRight];

Add those two methods to your UIViewController and add necessary actions:

- (IBAction)swipedRight:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"swiped right");
}

- (IBAction)swipedLeft:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"swiped left");
} 
Share:
27,457

Related videos on Youtube

Tomasz Szulc
Author by

Tomasz Szulc

Passionate iOS Software Engineer also writing developer-focused blog.

Updated on July 09, 2022

Comments

  • Tomasz Szulc
    Tomasz Szulc almost 2 years

    i need to detect direction of my swipe gesture and i've got problem with it. gesture is working, but i don't know how to detect direction. ...

    swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
    [swipeGesture setNumberOfTouchesRequired:1];
    [swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
    [appView addGestureRecognizer:swipeGesture];
    
    -(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer { 
    switch (recognizer.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"smth1");
            break;
    
    
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"smth2");
        default:
            break;
    }
    }
    

    it's not working :/

    • sosborn
      sosborn over 12 years
      Please define "it's not working." Does the log the incorrect value? Does it not log anything? Is detectSwipe not being called?
    • Tomasz Szulc
      Tomasz Szulc over 12 years
      default case is called when i swipe to up or to down.
    • bryanmac
      bryanmac over 12 years
      Since it's just an enum - have you tried to cast and log the value of the recognizer: developer.apple.com/library/IOs/#documentation/UIKit/Referen‌​ce/…
  • Tomasz Szulc
    Tomasz Szulc over 12 years
    CGPoint start = [swipeGesture locationInView:appView]; i use this and this function give me start point of swipe, but how i detect when swipe is ended? only way is to use touchesBegan and touchesEnded?
  • omz
    omz over 12 years
    I'm not sure if this is actually possible (you're probably better off with two recognizers). Check the state of the gesture recognizer in your action. For most gesture recognizers, it transitions from UIGestureRecognizerStateBegan to UIGestureRecognizerStateEnded, but it could be that the swipe gesture recognizer type doesn't do that, I haven't tried it.
  • Sumit Kumar Saha
    Sumit Kumar Saha over 6 years
    I think you don't need to add 4 gesture recognizer, just add one and in the didSwipe method check the sender's swipe direction, that should do it