Search code examples
iosswiftuiscrollviewuigesturerecognizer

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


I use a UIScrollView to show some images. I find both swipe gesture and pan gesture work. Is it possible to only make pan gesture work at a time?

I tried this code:

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

It doesn't work. I print scrollView.gestureRecognizers!, get the gesture list:

UIScrollViewDelayedTouchesBeganGestureRecognizer

UIScrollViewPanGestureRecognizer

_UIDragAutoScrollGestureRecognizer

UIScrollViewPagingSwipeGestureRecognizer

I tried UIScrollViewPagingSwipeGestureRecognizer:

if gestureRecognizer is UIScrollViewPagingSwipeGestureRecognizer) {
    gestureRecognizer.isEnabled = false
}

It says Use of undeclared type 'UIScrollViewPagingSwipeGestureRecognizer'.


Solution

  • If I understand you correctly... maybe you could use the following UIScrollViewDelegate method:

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