Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Press Gesture Recognizer Interfering With Scroll in UITableView

I'm having trouble adding a long press gesture to my UITableView. Well, technically, I do have a long press gesture recognizer, but I set the minimum taps duration to 0.08. I did this because I want to have the same general action for tapping and holding down a cell, but the action only changes based on how long the cell was held. Anyway, here is the code where I add the gesture recognizer (in viewDidLoad) :

    var longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    longPress.minimumPressDuration = 0.08
    longPress.delegate = self
    longPress.cancelsTouchesInView  = false
    self.tableView.addGestureRecognizer(longPress)
    self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(longPress)

In my handleLongPress() function, I get the CGPoint where there was a long press and then get the tableView cell from that.

So basically, if I scroll quickly, (like if I flick the screen), the table view scrolls fine. If I try to scroll slowly, the long press event fires and I can't scroll.

All I want to do is to be able to scroll slowly, I want the tableviews default scroll gesture to override any long press.

Thanks!

like image 468
codeforfood Avatar asked Mar 31 '15 22:03

codeforfood


1 Answers

ScrollViews have a panGestureRecognizer property, you can call requireGestureRecognizerToFail on your long press recognizer with the scrollView's panGestureRecognizer as an argument and it will only fire if the pan gesture in the scroll view fails.

like image 168
EmilioPelaez Avatar answered Oct 01 '22 08:10

EmilioPelaez