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

Is it possible to disable the swipe gesture and only make the pan gesture work in a scrollview?

我使用 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'

如果我没理解错的话...也许你可以使用下面的UIScrollViewDelegate方法:

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