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.
- How does UIKit decide which view receives a touch, and why can a subview outside its parent’s bounds not be tapped?
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.
- What is the responder chain, and what is the first responder for?
Everything that can handle events is a UIResponder: views, view controllers, the window, the application and its delegate. Each has a next responder, forming… - How do gesture recognisers interact with touches and with each other?
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
- 7.2 · UIView and CALayer, frame, bounds and center
- 7.3 · View controller lifecycle, containment, presentation and transitions
- 7.4 · Navigation, tab, split and page controllers
- 7.5 · Compositional layout, diffable data sources, cell reuse and prefetching
- 7.6 · UITableView, still asked and still everywhere
- 7.7 · Trait collections, size classes, safe area, keyboard and appearance
- 7.8 · Keyboard shortcuts, pointer, Apple Pencil and multitasking
- 7.9 · Hosting SwiftUI in UIKit and UIKit in SwiftUI
- 7.10 · Initialisers, stack views, scroll views and modern buttons
- 7.11 · Layout passes, retain cycles and menus
- 7.12 · Refreshing diffable cells, outlines and carousel layouts
- 7.13 · View-tree exercises and long navigation stacks