是否可以禁用滑动手势并仅使平移手势在滚动视图中工作?

Posted

技术标签:

【中文标题】是否可以禁用滑动手势并仅使平移手势在滚动视图中工作?【英文标题】:Is it possible to disable the swipe gesture and only make the pan gesture work in a scrollview? 【发布时间】:2019-05-13 06:45:53 【问题描述】:

我使用UIScrollView 来显示一些图像。我发现滑动手势和平移手势都有效。是否可以一次只使平移手势起作用?

我试过这个代码:

for gestureRecognizer in scrollView.gestureRecognizers ?? [] 
    if gestureRecognizer is UISwipeGestureRecognizer) 
        gestureRecognizer.isEnabled = false
    

它不起作用。我打印scrollView.gestureRecognizers!,获取手势列表:

UIScrollViewDelayedTouchesBeganGestureRecognizer

UIScrollViewPanGestureRecognizer

_UIDragAutoScrollGestureRecognizer

UIScrollViewPagingSwipeGestureRecognizer

我试过UIScrollViewPagingSwipeGestureRecognizer

if gestureRecognizer is UIScrollViewPagingSwipeGestureRecognizer) 
    gestureRecognizer.isEnabled = false

上面写着Use of undeclared type 'UIScrollViewPagingSwipeGestureRecognizer'

【问题讨论】:

你到底想达到什么目的? 感谢您的评论。我尝试通过仅拖动而不是滑动来使 scrollView 滚动。 【参考方案1】:

如果我对您的理解正确...也许您可以使用以下UIScrollViewDelegate 方法:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
    targetContentOffset.pointee = scrollView.contentOffset

【讨论】:

谢谢你,安德烈·斯洛塔。现在我认为它无法区分滚动视图中的拖动和滑动。

以上是关于是否可以禁用滑动手势并仅使平移手势在滚动视图中工作?的主要内容,如果未能解决你的问题,请参考以下文章

禁用 uicollectionview 水平滚动的外部平移手势

同一视图中平移和滑动手势之间的冲突?

REFrostedViewController 平移手势禁用 tableview 滑动

我可以在 tableviewcell 中发现 tableview 手势识别器的状态吗

使用平移手势识别器关闭视图控制器

使平移手势识别器仅在向下滑动时在桌面视图的顶部进行响应