Detect horizontal panning in UITableView

I had the same issue and came up with a solution that works with the UIPanGestureRecognizer.

In contrast to Erik I’ve added the UIPanGestureRecognizer to the cell directly, as I need just one particular cell at once to support the pan. But I guess this should work for Erik’s case as well.

Here’s the code.

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    UIView *cell = [gestureRecognizer view];
    CGPoint translation = [gestureRecognizer translationInView:[cell superview]];

    // Check for horizontal gesture
    if (fabsf(translation.x) > fabsf(translation.y))
    {
        return YES;
    }

    return NO;
}

The calculation for the horizontal gesture is copied form Erik’s code – I’ve tested this with iOS 4.3.

Edit:
I’ve found out that this implementation prevents the “swipe-to-delete” gesture. To regain that behavior I’ve added check for the velocity of the gesture to the if-statement above.

if ([gestureRecognizer velocityInView:cell].x < 600 && sqrt(translate...

After playing a bit on my device I came up with a velocity of 500 to 600 which offers in my opinion the best user experience for the transition between the pan and the swipe-to-delete gesture.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)