Cancel current UIScrollView touch

2019-02-09 09:40发布

问题:

I have an UIScrollView with a few subviews and so on. I am also the scrollView's delegate and have implemented the - (void)scrollViewDidScroll:(UIScrollView *)scrollView. Underneath my scroll there is another view.

I want to show that view if the scrollView's contentOffset goes under 50px on x axis, "reset" scrollView's contentOffset and cancel the current scrollView gesture so that the user wont manipulate its content when the new view appears.

I have implemented the method like so:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < -50)
    {
        scrollView.contentOffset = CGPointZero;
        [self showBackView];
        //here I want to cancel the current touch on the scrollview since it keeps scrolling if I drag my finger
    }
}

I have tried to set the userInteractionEnabled property to NO but it takes effect only after the touch has ended. And I have tried a bunch of other properties but none seems to work.

How can I fix this?

回答1:

Try disabling the panGestureRecognizer for the scroll view (and then reenabling it). This will cancel the current session of the recogniser:

ObjC

self.scrollView.panGestureRecognizer.enabled = NO;
self.scrollView.panGestureRecognizer.enabled = YES;

Swift

self.scrollView.panGestureRecognizer.isEnabled = false
self.scrollView.panGestureRecognizer.isEnabled = true