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

时间:2019-05-13 06:45:53

标签: ios swift uiscrollview uigesturerecognizer

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

1 个答案:

答案 0 :(得分:0)

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

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