iosswiftobjective-cfirst-responder

Whats the relation between First Responder and hitTest methods?


I understand how the system find the view handles touch events by calling the following methods on a view and its subviews

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

But I don't understand the role of first responder in this mechanism.

Does firstResponder represents the start point of the hitTest traverse?


Solution

  • I would recommend a complete reading of the first article

    Using Responders and the Responder Chain to Handle Events

    in Apple documentation

    Touches, Presses, and Gestures

    Short answer:

    1. Touch events are delivered directly to the first responder.

    When your app receives an event, UIKit automatically directs that event to the most appropriate responder object, known as the first responder.

    1. First responder is determined by hit-testing.

    UIKit uses view-based hit-testing to determine where touch events occur. Specifically, UIKit compares the touch location to the bounds of view objects in the view hierarchy. The hitTest(_:with:) method of UIView traverses the view hierarchy, looking for the deepest subview that contains the specified touch, which becomes the first responder for the touch event.

    1. If the first responder does not handle the event, the event is then passed from responder to responder in the active responder chain.