how detect swipe gesture direction?

前端 未结 4 2108
有刺的猬
有刺的猬 2020-12-02 21:06

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. ...

swipeGe         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 21:44

    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");
    } 
    

提交回复
热议问题