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); }
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With