6
votes

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!

2

2 Answers

1
votes

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.

0
votes

This category might solve your problem:

@interface UITableView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end

@implementation UITableView (CellSwipeAdditions)
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
@end