如何识别 UIScrollView 中的滑动手势
我正在尝试识别 UIScrollView
中的左/右滑动手势。我尝试创建 UISwipeGestureRecognizers
并将它们与滚动视图相关联。它有效,但很少见。大多数时候我不会接到电话。为什么?
我怎样才能可靠地向左/向右滑动来工作?我可以使用手势识别器还是我必须自己在 touchesBegan/Ended
中处理它
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
想通了。就我而言,我的 UIScrollView 包含一个允许缩放的 UIImage。显然,这意味着启用了滚动,并且 UIScrollView 无法区分滚动手势和滑动手势(下一张,上一张图片)。
在我的例子中,关键是当图像未放大时禁用滚动视图中的滚动,并在放大时重新启用它。这提供了预期的行为。
关键是将以下内容放入滚动视图的委托中:
我还必须在禁用滚动的情况下初始化滚动视图。
要启用缩放,只需在委托调用中提供图像,
并在 viewDidLoad 中为缩放设置一些参数并设置手势识别器
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
解决此问题的正确解决方案是添加一行代码:
Swift 版本:
Swift4 版本:
The correct solution to fix this issue is to add one line of code:
Swift version:
Swift4 version:
好帖子。
我正在做类似的事情(没有图像视图),如果 contentSize 小于高度,我基本上必须禁用滚动(我的滚动视图仅垂直滚动)。
这对我有用
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
适合那些想要制作动画并自定义滑动手势识别器的人。
我们可以使用 UIScrollView 和 UIGestureRecognizer 委托:
For those who want to animate and customize their swipe gesture recognizers.
We can use UIScrollView and UIGestureRecognizer delegates: