How to add a vertical swipe gesture to iPhone app for all screens?

后端 未结 2 1863
一整个雨季
一整个雨季 2021-01-02 06:19

I\'d like to add a gesture to my app so when the user swipes vertically it triggers a method to do something. The swipe can be up or down. I\'ve never done anything with g

相关标签:
2条回答
  • 2021-01-02 06:37

    This goes in ApplicationDidLaunch:

    UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)] autorelease];
    swipeGesture.numberOfTouchesRequired = 2;
        swipeGesture.direction = (UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown);
    
    [window addGestureRecognizer:swipeGesture];
    

    then implement

    - (void) swipedScreen:(UISwipeGestureRecognizer*)swipeGesture {
       // do stuff
    }
    

    Use the documentation for UIGestureRecognizer and UISwipeGestureRecognizer.

    Also if you wish to detect the direction of the swipe you will have to setup two separate gesture recognizers. You can not get the direction of a swipe from a swipe gesture recognizer, only the directions it is registered to recognize.

    0 讨论(0)
  • 2021-01-02 06:43

    In swift 4.0, that goes on the method didFinishLaunchingWithOptions of the AppDelegate:

    let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen(swipeGesture:)))
    swipeGesture.numberOfTouchesRequired = 2
    swipeGesture.direction = [UISwipeGestureRecognizerDirection.up,  UISwipeGestureRecognizerDirection.down]
    window?.addGestureRecognizer(swipeGesture)
    

    And the action:

    @objc func swipedScreen(swipeGesture: UISwipeGestureRecognizer){
        Swift.print("hy")
    }
    
    0 讨论(0)
提交回复
热议问题