The generation and delivery of events in iOS

Source: Internet
Author: User

Each time the code is broken and concatenated is a refactoring.

Https://www.jianshu.com/p/2e074db792ba

Generation and delivery of events in iOS 3.1. Event generation
    • When a touch event occurs, the system adds the event to a queue of events managed by UIApplication, and why is it a queue instead of a stack? Because the queue is characterized by FIFO, the first-in, first-out, generated events first processed before the common sense, so the event is added to the queue.
    • 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).
    • The main window finds the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process.
      Once the appropriate view control is found, the touches method of the view control is called to do the specific event handling.
3.2. Delivery of events
    • Touch event delivery is passed from parent control to child control
    • That is uiapplication->window-> looking for the most appropriate view for handling events.

注 意: Child controls cannot receive touch events if the parent control cannot accept touch events

How does the app find the most appropriate control to handle the event?
    • 1. First determine if the main window (Keywindow) can accept touch events
    • 2. Determine if the touch points are on their own
    • 3. Iterate through the child controls in the child control array, repeating the previous two steps (so-called to traverse the child control from the back, first to find the last element in the child control array, and then perform the 1, 2 steps)
    • 4.view, for example, called Fitview, will give this event to this fitview, and then traverse the Fitview child control until there is no more appropriate view.
    • 5. If there are no sub-controls that meet the criteria, then consider yourself most appropriate to handle the event, that is, you are the most appropriate view.

UIView cannot receive three cases of touch events:

    • no interaction allowed : userinteractionenabled = no
    • Hide : If you hide the parent control, the child controls are hidden, and the hidden control cannot accept the event
    • Transparency : If you set a control's transparency <0.01, it directly affects the transparency of the child controls. alpha:0.0~0.01 is transparent.

注 意: The default Uiimageview cannot accept touch events because the interaction is not allowed, that is, userinteractionenabled = No. So if you want Uiimageview to interact, you need to set Uiimageview's userinteractionenabled = YES.

Summarize

1. Click on a uiview or generate a touch event A, this touch event A will be added to the event queue managed by UIApplication (i.e., the first to receive the event is uiapplication).
2.UIApplication takes event A to the main window of the application (Keywindow) by removing the first event from the event pair column, which is assumed to be touch event A.
3. The window will find the most appropriate view in the view hierarchy to handle touch events. (At this point, the first step is complete)

Index.png

If you want a view to fail to handle the event (or the event is broken if it is passed to a view), you can go through the three ways just mentioned. For example, setting its userinteractionenabled = NO; Then the passed event will be handled by the view's parent control.
For example, if you do not want the blue view to receive events, then you can set the Blue view's userinteractionenabled = NO; then clicking on the yellow view or the Blue view will result in an orange view processing, The orange view will be the most suitable view.
So, regardless of whether the view can handle the event, just click on the view will produce an event, the key is that the event is ultimately handled by WHO! That is, if the blue view cannot handle the event, clicking on the Blue View produces a touch event that is not handled by the clicked view (Blue view)!
Note: If you set the transparency or hidden of a parent control, it directly affects the transparency and hidden of the child controls. If the parent control has a transparency of 0 or hidden = YES, then the child controls are not visible!

3.3. (difficult) How to find the most suitable view

How does the app find the most appropriate control to handle the event?
1. First determine if the main window (Keywindow) can accept touch events
2. Whether the touch point is on its own
3. Iterate through the child controls backwards, repeating the previous two steps (first looking at the last element in the array)
4. If there are no child controls that meet the criteria, consider yourself most suitable for handling

Detailed: 1. After the main window receives the event passed by the application, it first determines whether it can take over the touch event. If you can, then judge the touch point in the window itself
2. If the touch point is also on the window, the window will traverse its own child control (traversing its own child controls just to find the most appropriate view)
3. After iterating through each child control, the two steps above are repeated (passing the event to the child control, 1. Determines whether the child control can accept the event, 2. Point is not on child control)
4. Loop through the child controls so that the most appropriate view is found, and if there are no more appropriate sub-controls, then you will be the most appropriate view.
Once the most appropriate view is found, the view's touches method is called to handle the specific event. Therefore, only the most appropriate view is found, the event is passed to the most appropriate view, the touches method will be called to do the next event processing. If the most appropriate view is not found, the touches method is not called for event handling.
Note: The only way to look for the most appropriate view is to do some loop optimizations by taking the child controls from the back up. As compared to the next, the added view is above, reducing the number of cycles.



VV Wood Childe
Links: https://www.jianshu.com/p/2e074db792ba
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

The generation and delivery of events in iOS

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.