Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercepting pan gestures over a UIScrollView breaks scrolling

I have a vertically-scrolling UIScrollView. I want to also handle horizontal pans on it, while allowing the default vertical scroll behavior. I've put a transparent UIView over the scroll view, and added a pan gesture recognizer to it. This way I can get the pans just fine, but then the scroll view doesn't receive any gestures.

I've implemented the following UIPanGestureRecognizerDelegate methods, hoping to limit my gesture recognizer to horizontal pans only, but that didn't help:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {     // Only accept horizontal pans here.     // Leave the vertical pans for scrolling the content.     CGPoint translation = [gestureRecognizer translationInView:self.view];     BOOL isHorizontalPan = (fabsf(translation.x) > fabsf(translation.y));     return  isHorizontalPan; }  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {     return (otherGestureRecognizer == _scrollView.panGestureRecognizer); } 
like image 880
TotoroTotoro Avatar asked Dec 06 '12 03:12

TotoroTotoro


People also ask

What is a panning gesture?

A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.

How do you use pan gestures?

The user must press one or more fingers on a view while panning on the screen. A panning gesture is on continuous action when the user moves one or more fingers allowed (minimumNumberOfTouches) to enough distance for recognition as a pan.


1 Answers

OK, I figured it out. I needed to do 2 things to make this work:

1) Attach my own pan recognizer to the scroll view itself, not to another view on top of it.

2) This UIGestureRecognizerDelegate method prevents the goofy behavior that happens when both the default scrollview and my own one are invoked simultaneously.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {     return YES; } 
like image 180
TotoroTotoro Avatar answered Sep 24 '22 06:09

TotoroTotoro