Cancel current UIScrollView touch
Asked Answered
B

1

9

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?

Beale answered 28/8, 2013 at 20:2 Comment(2)
have you tried to set scrollEnabled to NO?Tercentenary
Yes, it did not work. Disabling the pan gesture recogniser does the job.Beale
P
37

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
Periodical answered 28/8, 2013 at 20:9 Comment(2)
This is great. I had a scenario with a UITableView with a UIRefreshControl that when pulled down, did an API call. In the event the API call errored out, there was an alert dialogue. The issue was that your touch is still on the UITableView so you could interact and call another API call which we did not want. Now with the UITableViews touch event cancelled, the user can't interact with the underlying table view because of the alert.Dworman
Thanks for this. I have a zoomable scrollview+imageview under a paginated scrollView. The problem was right after zooming out, you won't be able to scroll side ways to change the page. :) This solves that problem.Cockayne

© 2022 - 2024 — McMap. All rights reserved.