Search code examples
iosscrollgestureuitapgesturerecognizer

iOS: can I programmatically decide what component to interact with (scrolling)?


I'm in a situation where I have two scrollable views, one horinzontally and the other vertically. Is there a way to detect if the user did a vertical or horizontal gesture, and depending on that, choose the view to interact with?

Thank you for your help.


Solution

  • You can use scrollViewWillBeginDragging from UIScrollViewDelegate to get the direction on which your scroll will go.

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
        if translation.y > 0 {
            // swipes from top to bottom of screen -> down
        } else {
            // swipes from bottom to top of screen -> up
        }
    }
    

    But since this method doesn't return a scroll view, you will only know which scroll was dragged and to which direction.