The event transfer and response chains in iOS and ios

Source: Internet
Author: User

The event transfer and response chains in iOS and ios

First, let's take a look at the process of event generation and delivery in iOS:

1. After a touch event occurs, the system adds the event to a queue event managed by UIApplication.

2. UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, it sends the event to the application's main window (keyWindow) first)

3. The main window will find the most appropriate view in the view hierarchy to handle touch events.

4. After finding an appropriate view control, the touches method of the View control will be called to handle the event: touchesBegin... touchesMoved... touchesEnded

5. the default method of these touches methods is to transfer the event along the responder chain, and call the event a corresponding person for processing.

The following is an example to illustrate the specific transfer process,

Generally, events are transmitted from the parent control to the Child control.

For example, if you click a Green View, the transfer process is as follows: UIApplication-> Window-> White View-> Green View

Click the Blue View and the transfer process is as follows: UIApplication-> Window-> White View-> orange View-> Blue View

If the parent control cannot receive touch events, the Child control cannot receive touch events.

UIView cannot receive touch events in three cases:

1. Do not accept user interaction: userInteractionEnabled = NO;

2. Hide: hidden = YES;

3. Transparency: alpha = 0.0 ~ 0.01

How can we find the most appropriate control to handle events? The following guidelines apply:

1. Can I accept touch events?

2. Whether the touch points are on your own

3. traverse the child control from the back and repeat the two steps above

4. If there is no child control that meets the conditions, it is best for you to handle it.

For example:

The order of adding controls: White 1-> Green 2-> orange 2-> Blue 3-> Red 3-> yellow 4

Click the orange area here. The event delivery judgment process is as follows:

1. UIApplication extracts events from the event queue and delivers them to UIWindow.

2. You can use UIWindow to determine whether you can accept touch events.

3. UIWindow: Determine whether the touch point is on itself.

4. UIWindow facilitates its sub-control from the back to retrieve the White 1

5. Both of the preceding conditions are met in whitelist 1. traverse the sub-control orange 2.

6. Orange 2 meets the top two conditions. traverse the child control and retrieve the Red 3

7. Red 3 does not meet condition 2. Remove Blue 3.

8. Blue 3 does not meet condition 2. The most appropriate control is orange 2.

After finding the appropriate control, we need to respond. Here we will first introduce the responder chain: the responder chain is actually a combination of many responder objects (inherited from the UIResponder object ).

Generally, the control transfers the event along the responder chain and sends the event to the last responder for processing. So how can we determine who is the last responder of the current responder? There are two rules:

1. Determine whether the current View is a controller View. If it is a controller View, the previous responder is the controller.

2. If it is not a controller View, the previous responder is the parent control.

The response process is as follows:

If the Controller does not respond to the touches method, submit it to UIWindow. If the UIWindow does not respond, it is handed over to the UIApplication. If no response is made to the event, it will be voided.

In conclusion, the Transfer Response Process of a complete touch event is as follows:

UIApplication --> UIWindow --> recursively find the most suitable control --> the control calls the touches Method --> determines whether the touches method is implemented --> If the touches method is not implemented, the event is passed to the last responder by default --> find the last responder --> the method cannot be found to invalidate it.

PS: using the responder chain, we can call the touches super method to allow multiple responders to respond to the event at the same time.

 

Related Article

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.