Question 3
Responder chain and hit testing
How does hit testing decide which view receives a touch?
Answer outline
UIKit starts at the window and walks down the view tree. For each view it checks whether the point is inside, then asks the subviews from front to back. The deepest visible, interactive view that contains the point wins.
A view is skipped when it's hidden, has isUserInteractionEnabled set to false, or has an alpha below 0.01. A clear overlay with interaction enabled still blocks taps, which trips people up.
Hit testing only chooses the initial touch view. That UITouch stays attached to the same view for the whole touch sequence, even when the finger moves outside it. Gesture recognizers on that view or any of its superviews also see the touches and can cancel delivery to the view.
The responder chain is a separate path. Unhandled events and actions travel up from the view through its superviews, the view controller, the window, and the app. Which view was tapped is a hit testing question, and which responder handles keyboard input or a menu action is a responder chain question.
Principles
- Hit testing picks the initial touch target by walking the view tree from the window downward.
- Hidden views, views with interaction disabled, and views with alpha below 0.01 are never hit, but a clear view still is.
- Once a gesture recognizer recognizes, UIKit cancels delivery to the view, so
touchesBeganoverrides are rarely needed. - The responder chain carries unhandled events and actions upward, from view to view controller to window to app.



