Reprint Please specify source: http://blog.csdn.net/whjForWork/article/details/44926833
Introduction to Event handling event delivery:
After a touch event occurs, the system adds the event to an event queue managed by UIApplication, uiapplication the first event from the event queue and distributes the event for processing, typically Send an event to the application's main window first (Keywindow)
UIView does not accept three cases of touch events:
Do not receive user interaction
userInteractionEnabled = NO
Hide
hidden = YES
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.
Detailed procedure for event delivery:
- The main window will find the most appropriate view in the view hierarchy to handle touch events , but this is only the first step in the entire event process
- Once the appropriate view control is found, the touches method of the view control is called to do the specific event handling
Touchesbegan ...
Touchesmoved ...
Touchedended ...
- 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
Example:
Responder Chain
The complete process of event delivery
- First pass the event object from top to bottom (passed by the parent control to the child control), and find the most appropriate control to handle the event.
- Call the touches of the most appropriate control .... method
- If [super touches ...] is called, the event is passed up the responder chain to the previous responder
- The last responder's touches is then called .... method
How to determine the previous responder
- If the current view is a view of the controller, then the controller is the last responder
- If the current view is not the view of the controller, then the parent control is the last responder
The event passing process of the responder chain
- If view is a view of the controller, it is passed to the controller, and if not, it is passed to its parent view
- In the top-most view of the view hierarchy, if you cannot process the received event or message, it passes the event or message to the Window object for processing
- If the window object is not processed, it passes the event or message to the UIApplication object
- If UIApplication cannot process the event or message, it is discarded
Note Why you use queues to manage events instead of stacks?
Queue FIFO First, can ensure that the first occurrence of the event first processing. Stack advanced after out.
event handling in iOS development (ii)-event delivery, responder chain-