I. Event distribution processing "from outside to Inside"
After a touch occurs in iOS, events are added to the UIApplication event queue, which is UIApplication distributed from the event queue, and is typically distributed to the main window, where the main window invokes the hitTest:withEvent: method to find the appropriate event-triggering view, which is to find the touched View object
The search process is as follows:
- Invokes a method on the top-level view (Keywindow view) to
pointInside:withEvent: determine whether the touch point is within the current view;
- If no is returned, then Keywindow
hitTest:withEvent: returns nil;
- If yes is returned, it sends a message to all the child views of the current view, and the order in which it
hitTest:withEvent: traverses all the sub-views is traversed from the end of the subviews array ( traversing from the top of the interface);
- If there is a subview
hitTest:withEvent: return non-empty object, then Keywindow hitTest:withEvent: returns this object, processing ends;
- If all subview traversal ends still do not return a non-empty object, then Keywindow
hitTest:withEvent: returns the top-level view;
Second, the response chain "from inside to outside"
When you find a view object that is touched, you also need to determine whether the view object can handle the touch event, if it cannot be processed, and who should handle it, so the responder chain appears to find the event responder
The responder chain principle:
- When a touch object fails to respond to an
initalView event, it is passed to the ancestor view superView to respond
- If the parent view is not responding, continue to pass up
- Passed up to the view controller's root view
controllerView , if the root view is not responding, to the view controllerviewController
- The view controller does not respond, passed to the parent view controller's root view
superControllerView , and if the root view does not respond, pass to the parent view ControllersuperViewController
- The top-level view controller cannot respond and is passed to the main window
keyWindow
keyWindowCannot respond, pass to UIApplication processing
UIApplicationCannot respond, the event will be discarded
Iii. Succession of Uiresponder
The above event distribution and responder chain , do not need our concern, these operations are automatic execution, do not need us to operate, we just need to understand their principles on the line.
Not all classes in iOS can handle and accept events, only inherited UIResponder objects can handle events (which we often use, UIView UIViewController UIApplication inherit from UIResponder , they can receive and handle events ), but inherit UIResponder Doesn't mean it's going to handle events.
An object that inherits Uiresponder and cannot handle the case of an event:
userInteractionEnabled = NO;
hidden = YES;
alpha = 0 ~ 0.01;
- No implementation
touchesBegan:withEvent method
Override Uiresponder Touch Response method:
#pragma mark Touch is called at the beginning-(void) Touchesbegan: (nsset *) touches withevent: ( uievent *) event; #pragma mark is frequently called when Touch is moved-(void) touchesmoved: (nsset *) touches withevent: (uievent *) event; #pragma mark Touch ends when you leave the screen-(void) touchesended: (nsset *) touches withevent: (uievent *) event; #pragma mark Touch is called when the accidental cancellation occurs, such as when touching the phone in-(void) touchescancelled: ( Span class= "hljs-built_in" >nsset *) touches withevent: (uievent *) event;
Four, Uitouch object in Uiresponder Touch response method, need to get Uitouch object:
//取得一个触摸对象(对于多点触摸可能有多个对象)UITouch *touch = [touches anyObject]; //取得在指定视图的触摸位置CGPoint current = [touch locationInView:self.view];//取得在指定视图的前一个触摸位置CGPoint previous = [touch previousLocationInView:self.view];
Other Common properties:
- window: Touch
- View: Touch the viewing area
- Tapcount: Short Click times
Knowing this, you can use touch events to do something fun, (^o^)/~, where there is no specific code.
iOS Learning note 05-Touch Events