Nils Ziehn Nils Ziehn - 18 days ago 9
iOS Question

UiScrollview scrollviewDidScroll detect user touch

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.

Answer

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.

- (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;
        }
    }
}