I'm trying to recognize left/right swipe gesture in a UIScrollView
. I've tried to create UISwipeGestureRecognizers
and associate them with the scroll view. It works but very rarely. Most of the time I do not get called. Why?
How can I reliably get swiping left/right to work? Can I use the gesture recognizers or do I have to somehow handle it myself in touchesBegan/Ended
Thanks
The correct solution to fix this issue is to add one line of code:
Swift version:
Swift4 version:
Figured it out. In my case, my UIScrollView contained a UIImage that I allowed zooming. Apparently that meant that scrolling is enabled and the UIScrollView had trouble distinguishing between gestures intended to scroll vs. swipe (next, previous image).
The key in my case, is to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.
The critical piece is to put the following in the scroll view's delegate:
I also have to initialize the scroll view with scrolling disabled. To enable zooming, simply provide an image on a delegate call,
And set a few parms in viewDidLoad for the zooming and setup gesture recognizers as well
Good post.
I was doing a similar thing (no image view) and I basically had to disable scrolling if the contentSize was smaller than the height (my scroll view only scrolls vertical).
That did the trick for me