Send Response links for iOS events and send responses for ios events
When loading in iOS, the main function is executed first.
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}
Load UIApplication-> AppDelegate-> UIWindow-> UIViewController-> superView-> subViews based on the parameters of the main function.
Link: UIApplication. keyWindow. rootViewController. view. subView
Then, how does the system find the view for receiving touch events?
Call the hitTtest: withEvent of the Root View only through the search of UIView and its subclass. the execution process is as follows:
IOS uses hit-testing to find the touch view. Hit-Testing checks whether the touch point is within the associated view boundary, and if so, recursively checks all child views of the view. At the level, the view that is at lowest (the view closest to the user in my understanding) and has a border range containing touch points becomes the hit-test view. After determining the hit-test view, it passes the touch event to the view.
-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {// 1. determine whether the current control can receive the event if (self. userInteractionEnabled = NO | self. hidden = YES | self. alpha <= 0.01) return nil; // 2. if ([self pointInside: point withEvent: event] = NO) return nil; // 3. traverse your child control NSInteger count = self from the back. subviews. count; for (NSInteger I = count-1; I> = 0; I --) {UIView * childView = self. subviews [I]; // converts the coordinate system on the current control into the coordinate system CGPoint childP = [self convertPoint: point toView: childView] on the Child control; UIView * fitView = [childView hitTest: childP withEvent: event]; if (fitView) {// find the most appropriate view return fitView;} // The End of the loop, no view return self;} is more suitable than itself ;}
Where-(BOOL) pointInside :( CGPoint) point withEvent :( UIEvent *) event
This function is used to determine whether the current click or touch event point is in the current view.
It is called by hitTest: withEvent:. You can call pointInside: withEvent for each subview to determine which view to respond to this event. If PointInside: withEvent: YES is returned, the inheritance tree of the sub-view is traversed. (In the traversal order, the most responsive view is the view closest to the user. It starts from the top-level subview), that is, the subview OF the subview continues to call the recursion function until the subview that can respond is found (the hitTest: withEvent: will return self instead of nil); otherwise, the inheritance tree of the view will be ignored.