**UIResponder is an iOS API for handling user events. It can handle touch events, press events (3D touch), remote control events, hardware movement events. * * can touchesBegan, pressesBegan, motionBegan remoteControlReceivedWithEvent, get back to the corresponding message. _**UIResponder** is not only used to receive events, but also to process and pass the corresponding event, and if the current responder cannot handle it, it is forwarded to another appropriate responder for processing.

Applications receive and handle events through responders, which can be any subclass that inherits from UIResponder, such as UIView, UIViewController, UIApplication, and so on. When the event arrives, the system passes the event to the appropriate responder and makes it the first responder.

Events that are not handled by the first responder will be passed in the responder chain, and the rule of delivery is determined by the nextResponder of UIResponder. You can override this property to determine the rule of delivery. When an event arrives, the first responder does not receive the message and passes it back down the responder chain.

Find the first responder

Based on the API

There are two key apis for finding the first responder, and finding the first responder is done by constantly calling those apis of the subview.

Method is called to get the clicked view, which is the first responder.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
Copy the code

HitTest :withEvent: this method is called internally to determine whether the clicked area is on the view, and returns YES if it is, and NO if it is not.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
Copy the code

Find the first responder

When the application receives the event, it hands it tokeyWindowThe root view traverses the sub-views step by step according to the view hierarchy, and continuously judges the view scope during the traversal, and finally finds the first responder.

Starting with the keyWindow, we iterate through the subviews, calling UIView’s hitTest:withEvent: method, looking for the view in the clicked region, and calling the hitTest:withEvent: method of the subview that returns the view, and so on. If the subview is not in the click area or there is no subview, the current view is the first responder.

In the hitTest:withEvent: method, the subview is traversed from top to bottom and subViews’ pointInside:withEvent: method is called to find the topmost subview within the click area. If a subview is found, its hitTest:withEvent: method is called, and the process continues, and so on. If the subview is not in the click area, ignore the view and its subviews and continue traversing the other views.

You can override the corresponding method to control the traversal process. Make your own judgment by overriding the pointInside:withEvent: method and return YES or NO to see if the clicked area is on the view. Return the clicked view by overriding the hitTest:withEvent: method.

When traversing a view, this method ignores views in any of the following three cases, or if the view has any of the following characteristics. However, the background color of the view is clearColor, which is not ignored.

  1. The view ofhiddenEqual to YES.
  2. The view ofalphaLess than or equal to 0.01.
  3. The view ofuserInteractionEnabledTo NO.

If the click event occurs outside the view but inside its child view, the child view cannot receive the event and become the first responder. This is because it is ignored during the hitTest:withEvent: of its parent view.

events

Delivery process

  1. UIApplicationReceives the event and passes the event tokeyWindow.
  2. keyWindowtraversesubViewsthehitTest:withEvent:Method to find the appropriate view within the click area to handle the event.
  3. UIViewIt is also traversed by the child view ofsubViewsthehitTest:withEvent:Methods, and so on.
  4. Until you find the top view in the click area, step the view back toUIApplication.
  5. In the search for the first responder, a responder chain has been formed.
  6. The application calls the first responder to handle the event first.
  7. If the first responder cannot handle the event, it is callednextResponderMethod, always looking for an object in the responder chain that can handle the event.
  8. The last toUIApplicationIf no object can process the event, the event is discarded.