Recognize two fingers swipe down gesture in UITableView
Asked Answered
C

1

7

I want to add two fingers swipe up and down gestures in UITableView. The idea is to scroll through the cells using one finger pan gesture and do some other action by using two fingers swipe up/down gestures. I'd like to achieve a similar experience to Tweetbot's night mode toggle: https://vine.co/v/hF5J1Y7hubT

This is my code:

func setupGestureRecognizer() {

    swipeUp = UISwipeGestureRecognizer(target: self, action: "handleSwipe")
    swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipe")

    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down

    swipeUp.numberOfTouchesRequired = 2
    swipeDown.numberOfTouchesRequired = 2

    self.tableView.panGestureRecognizer.maximumNumberOfTouches = 1

    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)

    self.tableView.addGestureRecognizer(swipeUp)
    self.tableView.addGestureRecognizer(swipeDown)

}

func handleSwipe() {
    print("Swiped!")
    let alert = UIAlertController(title: "Gesture recognizer", message: "Swipe detected", preferredStyle: UIAlertControllerStyle.Alert)
    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)
}

setupGestureRecognizer() is called in viewDidLoad()

I get my alert when I swipe up or down with two fingers but when I use the pan gesture there's a significant lag before the table moves. It's probably the time pan gesture needs to wait to make sure swipe gesture fails:

It actually makes more sense to me to set requireGestureRecognizerToFail to this: swipeDown.requireGestureRecognizerToFail(self.tableView.panGestureRecognizer) but when I tried it the swipe gesture didn't work at all. I think there's a problem with panGestureRecognizer failing. Why doesn't is fail when I use two fingers if I explicitly stated that it should accept maximumNumberOfTouches = 1 ?

Do you know how to make these gestures interact with each other?

Cordiacordial answered 17/1, 2016 at 0:8 Comment(1)
Did you ever figure out the solution?Tedie
I
-1

Remove these lines

self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeUp)
self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(swipeDown)

Set the gesture delegate for swipeUp and swipeDown

swipeDown.delegate = self
swipeUp.delegate = self

And handle the gesture delegate for failure dynamically.

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Return true always. This will take care whether swipe is being handled or pan.

Importunate answered 17/1, 2016 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.