UiScrollview scrollviewDidScroll detect user touch

ios, uiscrollview

Solution

The `UIScrollView` has a `panGestureRecogniser` property, which tracks the pan gestures in the scroll view. You can track the `state` of the pan gesture in the `scrollViewDidScroll` to know exactly what state the panning is in.

Swift 4.0

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.isEqual(yourScrollView) {
        switch scrollView.panGestureRecognizer.state {
            case .began:
                // User began dragging
                print("began")
            case .changed:
                // User is currently dragging the scroll view
                print("changed")
            case .possible:
                // The scroll view scrolling but the user is no longer touching the scrollview (table is decelerating)
                print("possible")
            default:
                break
        }
    }
}

Objective-C

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if ([scrollView isEqual:yourScrollView]) {

        switch (scrollView.panGestureRecognizer.state) {

            case UIGestureRecognizerStateBegan:

                // User began dragging
                break;

            case UIGestureRecognizerStateChanged:

                // User is currently dragging the scroll view
                break;

            case UIGestureRecognizerStatePossible:

                // The scroll view scrolling but the user is no longer touching the scrollview (table is decelerating)
                break;

            default:
                break;
        }
    }
}

Problem

I am using a `UIScrollView` in my app, and use the `scrollviewDidScroll` method to move some other views that are not inside the scrollview. This behavior should only occur when the user is actually moving the scrollview with a finger and not when the touch event ends. Currently I am using the `dragging` property of the scrollview to get this behavior, but it has a problem: when the user swipes over the scrollview and touches it again before it stopped decelerating, the `dragging` property is not true although the user is dragging the scrollview with a finger.

Original source