How To Detect "Touch Down" in superview of UIScrollView?

wgpubs picture wgpubs · Apr 7, 2010 · Viewed 8.9k times · Source

I have a UIView that contains a UIScrollView and I want to be able to capture the "Touch Down" event in the UIView any time the user taps on the UIScrollView.

I've tried including all the touchesBegan/Ended/Cancelled handlers in my UIViewController but none of them get fired when tapping inside the UIScrollView contained in the main UIView.

What is the best way to accomplish this?

Answer

Arseniy Banayev picture Arseniy Banayev · Apr 8, 2010

In the UIView, implement touchesBegan:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // assign a UITouch object to the current touch
    UITouch *touch = [[event allTouches] anyObject];

    // if the view in which the touch is found is myScrollView
    // (assuming myScrollView is the UIScrollView and is a subview of the UIView)
    if ([touch view] == myScrollView) {
        // do stuff here
    }
}

A side note: make sure userInteractionEnabled is set to YES in the UIView.