IOS Event Delivery Response chain

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.