The main function is executed when loading in iOS
int Main (intChar * argv[]) { @autoreleasepool { return Class]));} }
Load Uiapplication->appdelegate->uiwindow->uiviewcontroller->superview->subviews According to the parameters of the main function
The relationship is: UIApplication.keyWindow.rootViewController.view.subView
So, how did the system find the view that received the touch event happening?
The hitttest:withevent of the root view is called only through UIView and its subclasses, and its execution is as follows:
iOS uses hit-testing to find the touch of a view. Hit-testing checks all child view of the view by checking whether the touch point is within the associated view boundary and, if so, recursively (recursively). A view that is lowest at the level (which I understand is the closest view from the user) and the boundary range contains the touch points becomes 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 if the current control can receive events if(self.userinteractionenabled = = NO | | self.hidden = = YES | | Self.alpha <=0.01)returnNil; //2. Determine if the point is not in the current control if([Self pointinside:point withevent:Event] = = NO)returnNil; //3. Traverse your own child controls from backwardsNsinteger count =Self.subviews.count; for(Nsinteger i = count-1; I >=0; i--) {UIView*childview =Self.subviews[i]; //converts the coordinate system on the current control to a coordinate system on a child controlCgpoint CHILDP =[self convertpoint:point toview:childview]; UIView*fitview = [Childview hittest:childp withevent:Event]; if(Fitview) {//find the most suitable view returnFitview; } } //the end of the loop means there is no more suitable view than yourself returnSelf ; }
Wherein,-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event
The purpose of this function is to determine whether the point of the current click or touch event is in the current view.
It is hittest:withevent: called by calling Pointinside:withevent on each child view: Determines which view ultimately responds to this event. If pointinside:withevent: Returns Yes, then the child view's inheritance tree is traversed (the first response in the traversal order is: The view closest to the user. It starts from the top-level subview), a child view of the child views, continues to call the recursive function until it finds a child view that can be responded to (the hittest:withevent of this child view: it returns self, not nil); The inheritance tree of the view is ignored.
IOS Event Delivery Response chain