Search code examples
iosuiscrollviewtouchesbegan

UIScrollView Subclass never receives touchesBegan message for swipes


I am trying to make a scroll view only scrollable on a certain region. To do this, I am subclassing UIScrollView and overriding touchesBegan (similar to this question).

Here's my (pretty simple) code.

.h

 @interface SuppressableScrollView : UIScrollView
 @end

.m

#import "SuppressableScrollView.h"

@implementation SuppressableScrollView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesBegan touches=%@ event=%@", touches, event);
    [super touchesBegan:touches withEvent:event];
}

@end

touchesBegan is only being called for touches that UIScrollView doesn't normally consume (like taps). Any idea how to intercept all of the touches?

I think I am missing a concept somewhere.


Solution

  • I was recently looking into something similar for UITableViews. UITableView is and extension of UIScrollView. In digging around inside it I discovered that there are 4 gesture recognisers attached to the UIScrollView to pick up swipes and other things. I would suggest dump out the gesture recognisers properties to see if any are being automatically created (which I think they are). In which case the only option I can think of is to remove them, but then the scroll view will not respond to gestures.

    So perhaps you need to look at those gesture recognisers and the gesture recogniser delegates you can use to see if there is a better place to hook into.

    P.S. gesture recognisers will automatically start swallowing events once they recogniser a gesture in progress.