I have a gesture recognizer on a UIScrollView, however it hardly ever gets called as the UIScrollView eats all the gestures.
I partially got around this issue with this line: [scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipe];
however, this line results in my recognizer always being accepted (the desired behavior) and the scroll view not scrolling.
That is, when you scroll, the recognizer is accepted but the view doesn't scroll.
How can I get around this, or is there an alternate solution?
Thanks!
A gesture recognizer operates on touches hit-tested to a specific view and all of that view's subviews. It thus must be associated with that view. To make that association you must call the UIView method addGestureRecognizer(_:) . A gesture recognizer doesn't participate in the view's responder chain.
The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.
Make a subclass of UIScrollView
. Add this method in your new subclass
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer { return YES; }
Make your scrollView class to your new scrollview subclass.
The way that works for me is subclass UIScrollView
and conform to the UIGestureRecognizerDelegate
in that subclass. Then call this method.
class ATScrollView: UIScrollView, UIGestureRecognizerDelegate { func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }
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