7.1 · Hit testing, the responder chain and gesture recognisers

Every touch goes on a two-step journey: first the system works out which view was touched, then it works out who handles it. Most “my button does not respond” bugs live in one of those two steps.

Mental model. Hit testing walks down the view tree to find the target. The responder chain walks up from it to find someone to act. They are different traversals with different rules.

Finding the view

How the target of a touch is chosen.

  1. How does UIKit decide which view receives a touch, and why can a subview outside its parent’s bounds not be tapped? 7.1.1 · L2
    When a touch begins, UIKit calls hitTest(_:with:) on the window, which recurses down the tree. For each view it asks: is isUserInteractionEnabled true, is it…

Handling the event

Who receives it, and how gesture recognisers compete.

  1. What is the responder chain, and what is the first responder for? 7.1.2 · L2
    Everything that can handle events is a UIResponder: views, view controllers, the window, the application and its delegate. Each has a next responder, forming…
  2. How do gesture recognisers interact with touches and with each other? 7.1.3 · L3
    A gesture recogniser is attached to a view and receives that view’s touches before the view does. It runs a state machine: possible until it decides, then…

More in UIKit