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, UIKit checks whether the point is inside it, then checks subviews from front to back. The deepest visible, interactive view that contains the point wins.
A view usually cannot receive the touch if it is hidden, has interaction disabled, or is effectively transparent. A clear overlay can still block taps if isUserInteractionEnabled is true.
Hit testing chooses the initial touch view; that UITouch stays associated with the same view for the touch sequence even if the finger moves outside. Gesture recognizers attached along that view path can also observe or cancel touches.
The responder chain is a separate fallback path: unhandled actions or events can travel from the view up through superviews, the view controller, window, and app. Don’t conflate 'which view was tapped' with 'which responder handles keyboard input.'
Principles
- Hit testing picks the initial touch target.
- Gesture recognizers and controls often handle touches before raw
touchesBegan-style responder forwarding matters. - The responder chain passes unhandled events/actions upward when needed.