iOS response chain and delivery mechanism

Source: Internet
Author: User

The main function is executed when loading in iOS

[OBJC]View PlainCopyprint?
    1. int main (int argc, charchar * argv[]) {
    2. @autoreleasepool {
    3. return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));
    4. }
    5. }

Load Uiapplication->appdelegate->uiwindow->uiviewcontroller->superview->subviews According to the parameters of the main function
The relationship is: UIApplication.keyWindow.rootViewController.view.subView
Event delivery Mechanism:
1. When a touch event occurs in an iOS program, the event is added to a task queue managed by the UIApplication
2.UIApplication distributes the events that are at the forefront of the task queue downward. That is UIWindow.
3.UIWindow distributes the event downward, i.e. UIView.
4.UIView first see if you can handle the event, whether the touch points on their own body. If you can, continue looking for child views.
5. Iterate over the child controls and repeat the above two steps.
6. If not found, then you are the event handler. If
7. If you cannot handle it, do not do any processing.
There are three main cases where UIView does not accept event handling:
1) Alpha <0.01
2) userinteractionenabled = NO
3.hidden = YES




The following comes from the network:
Responder chain Concept: When the iOS system detects a finger touch operation, it packs it into a Uievent object and puts it into the event queue of the currently active application. The uiapplication of a singleton takes a touch event from the event queue and passes it to the uiwindow of the Singleton, UIWindow object first uses the Hittest:withevent: method to find the view that contains the initial point of the touch operation, That is, you need to pass the touch event to its processed view, which is called Hit-test view.


Uiresponder is the base class for all response objects, and the interfaces that handle each of these events are defined in the Uiresponder class. The familiar uiapplication, Uiviewcontroller, UIWindow, and all Uikit classes that inherit from UIView are inherited directly or indirectly from Uiresponder, so their instances are the responder objects that can form the responder chain.


The UIWindow instance object first calls Hittest:withevent on its content view: This method calls Pointinside:withevent on each view in its view hierarchy: (This method is used to determine if the location of the click event is in the current view, to see if the user clicked the current view), and if pointinside:withevent: return yes, continue to cascade until you find where the touch operation occurred, This is the Hit-test view you are looking for.


Hittest:withevent: The process of the method is as follows:
First call the current view's Pointinside:withevent: method to determine whether the touch point is within the current view;
If no is returned, hittest:withevent: returns nil;
If yes, the Hittest:withevent: message is sent to all the child views of the current view (Subviews), and all child views are traversed in the order from the topmost view to the bottom view, which is the forward traversal from the end of the subviews array. Until a child view returns a non-empty object or all the child views are traversed;
If the child view returns a non-empty object for the first time, the Hittest:withevent: method returns this object, processing ends;
If all child views return non, the Hittest:withevent: method returns itself (self).


The process of delivering a complete touch event with a delivery response
UIApplication---UIWindow to find the most appropriate control for handling events
The control calls the touches method and determines whether the touches method is implemented, and no implementation defaults to passing the event to the previous responder--Finding the previous responder
PS: If it doesn't respond to uiapplication, then the event is discarded.
1. Responder chain: A chain that is linked together by a number of respondents
Responder: An object that inherits from Uiresponder is called a responder object
2. Previous responder (the default practice is to pass the event up the responder chain, handing the event to the previous responder for processing)
How can I tell who is the last responder of the current responder?
1> determines whether the controller is currently a view, and if so, the last responder is the controller
2> If the view is not currently a controller, the previous responder is the parent control
3. What is the use of the responder chain?
Allows multiple responders to respond to the event at the same time when a touch event is audible
Touchesbegan in the implementation file of the subclass: Add the following code to the method
[Super Touchesbegan:touches Withevent:event]

iOS response chain and delivery mechanism

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.