iOS Development UI Chapter-Event delivery
I. Generation and transmission of events
After a touch event occurs, the system adds the event to an event queue managed by UIApplication
UIApplication takes the first event from the event queue and distributes the event for processing, typically sending an event to the application's main window (Keywindow)
The main window will find the most appropriate view in the view hierarchy to handle touch events
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 ...
Ii. example of an event delivery process
Touch event delivery is passed from parent control to child control
Click Green View:uiapplication, UIWindow, White
Click on the Blue View:uiapplication, UIWindow, White, orange
Click on the Yellow view:uiapplication, UIWindow, White, orange
Note: child controls cannot receive touch events if the parent control cannot receive touch events
How do I find the most appropriate control to handle an event?
(1) Can I receive touch events?
(2) is the touch point on its own?
(3) Iterate through the child controls, repeating the previous two steps
(4) If there are no child controls that meet the criteria, then you are best suited to handle
Example:
Set up the following interface, each of which is managed by a single class for each custom view.
Create a new base class that inherits from view, and let other custom view inherit from the class, overriding the following code in the class.
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
NSLog (@ "%@", Self.class);
}
Iii. three cases of not accepting touch events
(1) Do not receive user interaction userinteractionenabled = no
(2) Hide hidden = YES
(3) Transparent alpha = 0.0 ~ 0.01
Tips: Uiimageview's userinteractionenabled default is no, so Uiimageview and its child controls cannot receive touch events by default.