How can I force a UIScrollView
in which paging and scrolling are on to only move vertically or horizontally at a given moment?
My understanding is that
I've also had to solve this problem, and while Andrey Tarantsov's answer definitely has a lot of useful information for understanding how UIScrollViews
work, I feel that the solution is a bit over-complicated. My solution, which doesn't involve any subclassing or touch forwarding, is as follows:
UIPanGestureRecognizers
, again one for each direction.UIScrollViews
' panGestureRecognizer properties and the dummy UIPanGestureRecognizer
corresponding to the direction perpendicular to your UIScrollView's desired scrolling direction by using UIGestureRecognizer's
-requireGestureRecognizerToFail:
selector.-gestureRecognizerShouldBegin:
callbacks for your dummy UIPanGestureRecognizers, calculate the initial direction of the pan using the gesture's -translationInView: selector (your UIPanGestureRecognizers
won't call this delegate callback until your touch has translated enough to register as a pan). Allow or disallow your gesture recognizer to begin depending on the calculated direction, which in turn should control whether or not the perpendicular UIScrollView pan gesture recognizer will be allowed to begin.-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
, don't allow your dummy UIPanGestureRecognizers
to run alongside scrolling (unless you have some extra behavior you wanted to add).