Generation and delivery of events in iOS

Every time the code is broken up and then concatenated is a refactoring.

 

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

 

Generation and delivery of events in iOS

3.1. Generation of events

  • After a touch event occurs, the system will add the event to an event queue managed by UIApplication. Why is it a queue instead of a stack? Because the characteristic of the queue is FIFO, that is, first-in, first-out, it is common sense to process the event generated first, so the event is added to the queue.
  • UIApplication will take the front event from the event queue and distribute the event for processing. Usually, the event is sent to the main window (keyWindow) of the application first.
  • The main window will find the most appropriate view in the view hierarchy to handle the touch event, which is the first step in the entire event handling process.
    After finding a suitable view control, the touches method of the view control will be called for specific event processing.

3.2. Delivery of events

  • The delivery of touch events is from the parent control to the child control
  • That is, UIApplication->window->find the most appropriate view to handle events

注 意: If the parent control cannot receive touch events, then it is impossible for the child controls to receive touch events

How does the app find the most appropriate control to handle the event?
  • 1. First determine whether the main window (keyWindow) itself can accept touch events
  • 2. Determine whether the touch point is on yourself
  • 3. Traverse the sub-controls from back to front in the sub-control array, and repeat the previous two steps (the so-called traversing the sub-controls from the back to the front is to first find the last element in the sub-control array, and then perform steps 1 and 2)
  • 4. view, such as called fitView, will pass this event to the fitView, and then traverse the child controls of the fitView until there is no more suitable view.
  • 5. If there is no sub-control that meets the conditions, then you think that you are the most suitable to handle this event, that is, you are the most suitable view.

There are three situations in which UIView cannot receive touch events:

  • Interaction not allowed : userInteractionEnabled = NO
  • Hidden : If the parent control is hidden, the child control will also be hidden, and the hidden control cannot accept events
  • Transparency : If the transparency of a control is set to < 0.01, it will directly affect the transparency of the child controls. alpha: 0.0~0.01 is transparent.

注 意: The default UIImageView cannot accept touch events because interaction is not allowed, i.e. userInteractionEnabled = NO. So if you want UIImageView to be interactive, you need to set UIImageView's userInteractionEnabled = YES.

in conclusion

1. Click on a UIView or generate a touch event A, which will be added to the event queue managed by UIApplication (that is, UIApplication is the first to receive the event).
2. UIApplication will take the foremost event from the event pair column (assuming touch event A here), and pass event A to the main window (keyWindow) of the application.
3. The window will find the most appropriate view in the view hierarchy to handle the touch event. (So ​​far, the first step has been completed)

 
index.png

If you want a view to not be able to handle the event (or, in other words, the event will be interrupted when it is delivered to a view), then you can use the three methods just mentioned. For example, set its userInteractionEnabled = NO; then the passed events will be handled by the view's parent control.
For example, if you don't want the blue view to receive events, you can set the blue view's userInteractionEnabled = NO; then the events generated by clicking on the yellow view or the blue view will eventually be handled by the orange view, and the orange view will will be the most suitable view.
Therefore, no matter whether the view can handle the event or not, as long as the view is clicked, an event will be generated. The key is who handles the event in the end! That is, if the blue view cannot handle the event, the touch event generated by clicking on the blue view will not be handled by the clicked view (the blue view)!
Note: If you set the transparency or hidden of the parent control, it will directly affect the transparency and hidden of the child control. If the transparency of the parent control is 0 or hidden = YES, then the child control is also invisible!

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

How does the app find the most appropriate control to handle the event?
1. First determine whether the main window (keyWindow) itself can accept touch events
2. Whether the touch point is on itself
3. Traverse the child controls from back to front, repeat the previous two steps (first find the last element in the array)
4. If there are no eligible child controls, then consider yourself the most appropriate to deal with

Details: 1. After the main window receives the event passed by the application, it first determines whether it can take over the touch event. If it can, then judge whether the touch point is on the window itself
   2. If the touch point is also on the window, then the window will traverse its own child controls from back to front (traversing its own child controls is just to find the most suitable view)
   3. After traversing to each sub-control, the above two steps will be repeated (pass the event to the sub-control, 1. Determine whether the sub-control can accept the event, 2. The point is not on the sub-control)
   4. This loop traverses the sub -control Controls until the most suitable view is found. If there is no more suitable child control, then you will become the most suitable view.
After finding the most suitable view, the touches method of the view will be called to handle specific events. Therefore, only after the most suitable view is found and the event is passed to the most suitable view, the touches method will be called for subsequent event processing. If the most suitable view is not found, the touches method will not be called for event processing.
Note: The reason why we will traverse the child controls from the back to the front to find the most suitable view is just to do some loop optimization. Because in comparison, the view added later is on the top, reducing the number of loops.



Author: VV Mu Gongzi
Link : https://www.jianshu.com/p/2e074db792ba
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324627603&siteId=291194637