Recognize two fingers in a gesture in a UITableView

I want to add two fingers up and down in a UITableView. The idea is to scroll cells with one finger hand gesture and do some other actions using two finger swipe up / down gestures. I would like to get a similar experience in Tweetbot night mode: 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 warning when I scroll up or down with two fingers, but when I use the pan gesture, there is a significant lag before the table moves. Probably, for this it is necessary that the gesture of a tough time should have to wait to make sure that the chewing gesture was not successful:

Actually, it makes more sense for me to set requireGestureRecognizerToFail to this: swipeDown.requireGestureRecognizerToFail(self.tableView.panGestureRecognizer) , but when I tried this, the swipe does not work at all. I think the problem with panGestureRecognizer not running. Why is this not a failure when I use two fingers if I explicitly stated that it should take maximumNumberOfTouches = 1 ?

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

+5
source share
1 answer

Remove these lines

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

Set gesture delegate for swipeUp and swipeDown

 swipeDown.delegate = self swipeUp.delegate = self 

And process the delegate gestures for rejection dynamically.

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

Always return true. This will take care of whether the napkin or pan is being processed.

-1
source

Source: https://habr.com/ru/post/1240780/


All Articles