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?