Chapter 5. Touches
[Winifred the Woebegone illustrates hit-testing:] Hey nonny nonny, is it you? — Hey nonny nonny nonny no! — Hey nonny nonny, is it you? — Hey nonny nonny nonny no!
Marshall Barer, Once Upon a Mattress
A touch is an instance of the user putting a finger on the screen. The system and the hardware, working together, know when a finger contacts the screen and where it is. A finger is fat, but its location is cleverly reduced to a single point.
A UIResponder is a potential recipient of touches. A UIView is a UIResponder and is the visible recipient of touches. There are other UIResponder subclasses, but none of them is visible on the screen. The user sees a view by virtue of its underlying layer; the user touches a view by virtue of the fact that it is a UIResponder.
A touch is represented as an object (a UITouch instance) that is bundled up in an envelope (a UIEvent) that the system delivers to your app. It is then up to your app to deliver the envelope to the appropriate UIView. In the vast majority of cases, this will happen automatically the way you expect, and you will respond to a touch by way of the view in which the touch occurred.
Most built-in interface views deal with these low-level UITouch deliveries for you. They analyze and reduce the touches, and then notify your code at a higher level — you hear about functionality and intention rather than raw touches:
-
A UIButton reports that it was tapped.
-
A UITextField reports that its text changed.
Get Programming iOS 14 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.