Search code examples
iosobjective-cuiviewuiscrollviewtouch-event

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


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?


Solution

  • 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.