How does UIScrollView steal touches from its subviews?

cocoa-touch, iphone, uiscrollview

Solution

You can implement `hitTest:withEvent:` method in your `UIView` subclass.

This method gets called to check what subview must receive the touch event, so you can perform some action there before it actually happens. You can also change subview that must receive event.

Problem

I'd like to be able to create my own container subclasses of UIView which can react first to touches, before their subviews. This is tricky because normally a subview receives touch events (via `touchesBegan:` etc) before superviews. How does UIScrollView reverse this? To be clear, I am not asking how UIScrollView behaves. I understand what it does, and how you would normally use it. I'm asking about how I could cleanly implement my own version of this -- not because I want to, but because I'm trying to build reusable container views that take advantage of similar behavior.

Original source