Search code examples
iosswiftuiscrollviewuigesturerecognizeruiswipegesturerecognizer

Recognize swipe gesture in UIScrollView only when scrolling content reach the edge


I have UIScrollView with vertical scroll active. What I am trying to do is to add a swipe gesture with direction .down which will be recognized when the user cannot scroll the content anymore because it reaches the edge.

I was trying with require(toFail:) but it doesn't work properly.

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
swipeDown.require(toFail: self.scrollView.panGestureRecognizer)
self.scrollView.addGestureRecognizer(swipeDown)

I have also added UIGestureRecognizerDelegate method to recognize simultaneously:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
}

How to give always priority to scrolling content inside scrollView and when it is not possible anymore to detect swipe?


Solution

  • OK, the way I have managed that issue was simply check if the contentOffset reaches point 0.0, and if yes then disable scrolling and activate additional gesture. For my case that was enough.

    if scrollView.contentOffset.y == 0.0 {
        print("content on top")
    
        // use delegate method here to manage the gestures
    
    }