iOS event delivery and response process

Source: Internet
Author: User

iOS event delivery and response process –> Event arrival –> Event Distribution –> Event response Event (events) 1. Touch Events (single-touch, multi-touch and various gestures) 2. Shaking events (Motion events) (Gravity, Acceleration and other sensors) 3. Remote control Events (Remote-control events) (line headset, airplay) event distribution (event Delivery) Touch events: Hardware interrupts, touch Uikit encapsulated as Uievent object (for touch events), event queue for currently running applications, uiapplication->key window->hit-testing View other events: Key window First Responder Purpose (official document)

The ultimate goal of these event paths is to find a object that can handle and respond to an event. Therefore, UIKit first sends the event to the object that's best suited to handle the event. For touch events, this object is the Hit-test view, and for other events, this object is the first responder.

Method: Hit-testing Returns The View Where a touch occurred touch

The Hit-test view is given the first opportunity to handle a touch event. If The Hit-test view cannot handle an event, the event travels up, the view ' s chain of responders as described in "the Re Sponder Chain is made up of Responder Objects "until the system finds a object that can handle it.

The Responder Chain is made an event delivery description of the Responder Objects (Motion Or r-c) event that occurs after a touch event, the event is added to a uiapplication-managed event queue. UIApplication takes the first event out of the event queue and distributes the event for processing, typically Send events first to the application's main window (Keywindow) UIView does not accept touch events in three scenarios: 1. When user interaction is not received: userinteractionenabled = no;2. When the view is hidden: hidden = yes;3. When the view is transparent: Alpha = 0.0~0.01 Note: Uiimageview's userinteractionenabled default is no, So Uiimageview and its child controls are by default a detailed procedure for event delivery that cannot receive touch events: When a touch occurs, the main window finds the most appropriate view in the view hierarchy to handle the touch event, but this is only the first step in the entire event processing process, and after the appropriate view control is found, The touches method of the view control is invoked to do the specific event handling of the start Touch event:-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Move event when Touch is touched:-(void) Touchesmoved: (Nsset *) touches withevent: (Uievent *) event Touch End events:-(void) touchesended: (Nsset *) touches withevent: ( Uievent *) Event The default practice of these touches methods is to pass the time up the response chain, handing the event over to the previous responder for processing. Example:

Example of a responder chain:

The complete process of event delivery now passes the event object from top to bottom (passed by the parent control to the child control), and finds the most appropriate control to handle the event. Call the touches of the most appropriate control .... method if a [super touches ...] is called, the event is passed up the responder chain, passed to the previous responder, and then called the previous responder's touches .... Method How to determine the last responder if the current view is the controller's view, then the controller is the last responder if the current view is not the controller's view, then the parent control is the event delivery process of the previous responder's chain if view is the view of the controller, is passed to the controller, if it is not, it is passed to its parent view in the top-most view of the view hierarchy, and if it cannot process the received event or message, it passes the event or message to the Window object for processing if the window object is not processed. Then it passes the event or message to the UIApplication object, and if UIApplication cannot handle the event or message, discard it to note why the event is managed with a queue instead of the stack. Queue FIFO First, can ensure that the first occurrence of the event first processing. Stack advanced after out. Code Show:

//Specific features://1. Each view can be dragged by touch, and the animated effect of the method in touch is implemented in the (Touchesbegin method). //2. When the touch ends there is an animated effect of zooming in and out of the restore (implemented in the Touchesend method). //3. When you touch any of these views, the view moves along with the gesture, and when the center point of the Mobile view (View.center) enters the frame of the other two views, the two views are adsorbed, and finally there appears to be only one view left (implemented in the Touchesmove method). //4. When you double-click any space in the parent view, three views return to the starting position with the effect of the animation (implemented in the Touchesbegin method)#import "ViewController.h"  @interface viewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*magentaview;@property(Weak,nonatomic)Iboutlet Uiimageview*cyanview;@property(Weak,nonatomic)Iboutlet Uiimageview*yellowview;@end#define KMAGENTAVIEWFRAME (cgrect) {53,55,100,100}#define KCYANVIEWFRAME (cgrect) {132,254,100,100}#define KYELLOWVIEWFRAME (cgrect) {223,457,100,100} @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];}//Custom method to find the location of the touch point- (Cgpoint) Locationintouches: (Nsset *) touches{//0. Remove the touch firstUitouch *touch = [touches anyobject];//1. Return to Touch Point (note: coordinate system Self.view)    return[Touch Locationinview: Self. View];}//Custom method to determine if the touch point is in a frame of three views, yes is returned, and the view moves with the gesture. - (BOOL) Isimageviewscontainspoint: (Cgpoint) location{if(Cgrectcontainspoint (_magentaview. Frame, location) | | Cgrectcontainspoint (_cyanview. Frame, location) | | Cgrectcontainspoint (_yellowview. Frame, location)) {return YES; }return NO;}//Custom method, when double-click, three views back to their original position- (void) resetframes{[UIViewAnimatewithduration:0.5animations:^{_cyanview. Frame= Kcyanviewframe; _magentaview. Frame= Kmagentaviewframe; _yellowview. Frame= Kyellowviewframe; }];} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{CgpointLocation = [ SelfLocationintouches:touches];//0. Remove the touch firstUitouch *touch = [touches anyobject];if([ SelfIsimageviewscontainspoint:location]) {//Touch occurs in three views        //1. Remove the view that the touch occurred        Uiimageview*view = (Uiimageview*) Touch. View;//2. Set the center point of the touch view as the touch point and zoom in on the touch view[UIViewAnimatewithduration:0.5animations:^{View. Center= Location; View. Transform= Cgaffinetransformmakescale (1.2,1.2);    }]; }Else{//Perform a double-click Operation        if(Touch. Tapcount==2) {//double-click[ SelfResetframes]; }    }}- (void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{CgpointLocation = [ SelfLocationintouches:touches];//Move the touch point in which view is in the rectangular area, the center point of the view is set to the touch point    if(Cgrectcontainspoint (_magentaview. Frame, location)) {[UIViewAnimatewithduration:0.3animations:^{_magentaview. Center= Location;    }]; }if(Cgrectcontainspoint (_cyanview. Frame, location)) {[UIViewAnimatewithduration:0.3animations:^{_cyanview. Center= Location;    }]; }if(Cgrectcontainspoint (_yellowview. Frame, location)) {[UIViewAnimatewithduration:0.3animations:^{_yellowview. Center= Location;    }]; }}- (void) touchesended: (Nsset *) touches withevent: (uievent *) event{//0. Remove the touch firstUitouch *touch = [touches anyobject];//1. Remove the view that the touch occurred    Uiimageview*view = (Uiimageview*) Touch. View;//restore Touch View[UIViewAnimatewithduration:0.5animations:^{View. Transform= Cgaffinetransformidentity; }];}@end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS event delivery and response process

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.