Responder chain
An application can handle different kind of events thanks to responder objects. These objects implement specific methods that are useful to respond to one or more events. The UIResponder
class is the base class where an interface for these events is defined; therefore, most of the classes that are directly exposed to user interaction are subclasses of UIResponder
, such as UIView
, UIViewController
, UIControl
, and even the UIApplication
class.
A touch event is propagated through the application structure following an explicit path: the event is generated by the system and added into the event queue; the UIApplication
singleton gets the topmost event and propagates it to the key window that, in turn, identifies the view where the touch occurred and passes the event to this view.
Hit-testing
The process through which this view is defined is called hit-testing, and it mainly relies on the hitTest(_:withEvent:)
method. The logic is extremely simple: given a touch position, the hit...