0
votes

I have a scrollview that is limited to scrolling vertically. Inside of it I was wanting to have views that have a UIPanGestureRecognizer to only recognize horizontal pans.

As is the horizontal recognizers win and prevent the scrollview from scrolling at all.

I want the horizontal pan to win if it's detecting a mostly horizontal gesture, otherwise the vertical scroll should win. Very similar to how Mailbox works, or swiping in iOS8 Mail.app

1
I'm kind of shocked I haven't been able to find this after extensive searching. So apologies if it's already been answered.Tal
In what cases do you want horizontal panning to win and in what cases do you want vertical?Lyndsey Scott
Updated question to describe my issue.Tal

1 Answers

4
votes

You could use one of the UIGestureRecognizerDelegate methods like gestureRecognizerShouldBegin: to specify which pan gesture is triggered in which situation.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    // If the gesture is a pan, determine whether it starts out more
    // horizontal than vertical than act accordingly
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {

        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint velocity = [panGestureRecognizer velocityInView:self.view];

        if (gestureRecognizer == self.scrollView.panGestureRecognizer) {
            // For the vertical scrollview, if it's more vertical than
            // horizontal, return true; else false
            return fabs(velocity.y) > fabs(velocity.x);
        } else {
            // For the horizontal pan view, if it's more horizontal than
            // vertical, return true; else false
            return fabs(velocity.y) < fabs(velocity.x);
        }
    }
    else
        return YES;
}