A brief analysis of the responders ' chain

Source: Internet
Author: User

When it comes to the responder chain, the first thing to do is touch events and responders. Touch events don't explain, so what is a responder?I. RespondentsNot all objects in iOS can handle events, only objects that inherit Uiresponder can receive and handle events. We call it "responder object." For example:uiapplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are both responder objects that can receive and handle events. ii. Transmission of events With touch events and responders, the responder chain is created. After a touch event occurs, the system adds the event to an event queue managed by UIApplication (why the queue is not a stack, because the queue is sequentially in and out, and the stack is advanced). UIApplication takes the first event out of the event queue and distributes the event for processing, typically sending an event to the application's main window Keywindow.      From this, we begin to find the most suitable responders from low to top, and then find the responders who can handle the events from the top to the low to produce the responder chain. The responder chain is a chain that is connected by multiple responder objects. The connection between each responder can be clearly seen, and one event can be processed by multiple objects. The process of sending and returning events is the passing of events.third, how to find the most appropriate responder there are a few steps:1> can I receive touch events?     No, the event is passed to this end. 2> touch points on their own?     No, the event is passed to this end.     3> iterates the child controls from the back, repeating the previous two steps.     4> If there are no child controls that meet the criteria, then you are best suited to handle them.     Note 1: If the parent control cannot receive touch events, then the child controls will not be able to receive touch events! NOTE 2: UIView do not receive three cases of touch events? 1 "Do not receive user interaction, userinteractionenabled = no; 2 "hidden, hidden = YES;     3 "Transparent, alpha = 0.0 ~ 0.01; Tip: Uiimageview's userinteractionenabled default is no, so Uiimageview and its child controls cannot receive touch events by default.Iv. Finding responder processes that can handle touch events 1, after the user taps the screen to produce a touch event, after a series of transmission process, will find the most appropriate responder. Once the most appropriate responder is found, the control's touches method is called to do the specific event handling. Touchesbegan, touchesmoved, touchedended, and so on, the default practice of these touches methods is to pass the event up the responder chain, handing the event to the previous responder for processing. Here you can get the complete process of event delivery:1> to find the most appropriate responder by passing the event object from low to top (passed by the parent control to the child control). 2> calls the touches of the most appropriate responder .... method to handle the event. 3> If [super touches ...] is called, the event is passed down the responder chain and passed to the previous responder. 4> then calls the last responder's touches .... method. 2, the responder chain to find the last responder 3, to determine the previous responder note 1> If the current view is the controller's view, then the controller is the previous responder 2> if the current view is not the controller's view, then the parent control is the previous A responder 4. Delivery Events Note 1> If the view controller exists, it is passed to the controller, and if the controller does not exist, it is passed to its parent view 2> in the top-level view of the view hierarchy, and if it cannot process the received event or message, it passes the event or message to the Window object for processing 3&gt If the Window object is also not processed, it passes an event or message to the UIApplication object 4> if UIApplication cannot handle the event or message, it is discarded.v. Introduction of two methods1,-(UIView *) HitTest: (cgpoint) point withevent: (Uievent *) event; In the process of finding the most appropriate responder, the method is called by each responder that receives the event. By overriding this method and return nil, you can intercept the delivery of the event and use the previous responder as the most appropriate responder.     You can also return a specific control, for example: return self.subviews[0].        2,-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event; In the process of finding the most appropriate responder, the judgment point is not above the method caller, that is, the point is not on the current control.     Override method, return NO, and also intercept event delivery. Using these two methods together, you can achieve magical results.

A brief analysis of the responders ' chain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.