how detect swipe gesture direction?

前端 未结 4 1802
有刺的猬
有刺的猬 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条回答
  • 2020-12-02 21:36

    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.

    0 讨论(0)
  • 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");
        }
    }
    
    0 讨论(0)
  • 2020-12-02 21:41

    Swipe Gesture direction detect with Swift 5

        override func viewDidLoad() {
        super.viewDidLoad()
    
             let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
             let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
    
             leftSwipe.direction = .left
             rightSwipe.direction = .right
    
             view.addGestureRecognizer(leftSwipe)
             view.addGestureRecognizer(rightSwipe)
        }
    
        @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
    
            if (sender.direction == .left) {
                    print("Swipe Left")
            }
    
            if (sender.direction == .right) {
                print("Swipe Right")
            }
        }
    
    0 讨论(0)
  • 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");
    } 
    
    0 讨论(0)
提交回复
热议问题