Event Handling Responder Object
Not all objects in iOS can handle events, only objects that inherit Uiresponder can receive and handle events. We call it "responder object."
UIApplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are both responder objects that can receive and handle events
Uiresponder internally provides the following methods to handle events
- These classes can override the following several methods to handle different touch events ' OBJC//Touch events
- (void) Touchesbegan: (Nsset ) touches withevent: (uievent ) event;
- (void) touchesmoved: (Nsset ) touches withevent: (uievent ) event;
- (void) touchesended: (Nsset ) touches withevent: (uievent ) event;
- (void) touchescancelled: (Nsset ) touches withevent: (uievent ) event;
Accelerometer Events
- (void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event;
- (void) motionended: (uieventsubtype) Motion withevent: (uievent *) event;
- (void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event;
Remote control Events
- (void) Remotecontrolreceivedwithevent: (Uievent *) event; ```
The role of the parameter Uitouchuitouch of the event
- Holds information about the finger, such as the location, time, and stage of the touch
- When the finger moves, the system updates the same Uitouch object so that it can keep the finger in the touch position
- When the finger leaves the screen, the system destroys the corresponding Uitouch object
Methods of Uitouch
- (CGPoint)locationInView:(UIView *)view;
- The return value indicates the position of the touch on the view
- The position returned here is for the view's coordinate system (in the upper left corner of the view is the origin (0, 0))
- When called, the view parameter is nil, which returns the position of the touch point in the UIWindow.
- (CGPoint)previousLocationInView:(UIView *)view;
- This method records the position of the previous touch point
Uievent
- Each occurrence of an event produces a Uievent object
- Uievent: Called an event object to record the time and type of event generation
Common attribute Event types
@property(nonatomic,readonly) UIEventType type;@property(nonatomic,readonly) UIEventSubtype subtype;
Time the event was generated
@property(nonatomic,readonly) NSTimeInterval timestamp;
Generation and delivery of events
-
- After a touch event occurs, the event is added to an event queue that is managed by uiapplication.
- 2.UIApplication removes the first event from the event queue and distributes the event for processing, typically by sending an event to the application's main window (Keywindow)
- 3. The main window will find the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process.
- 4. Once the appropriate view control is found, the touches method of the view control is called to make specific event handling
touchesBegan…touchesMoved…touchedEnded…
If the parent control cannot receive touch events, then the child control cannot receive a touch event (mastering) How to find the most appropriate control to handle the event?
- Are you able to receive touch events?
- Is the touch point on your body?
- Iterate through the child controls backwards, repeating the previous two steps
- If there are no child controls that meet the criteria, then you are best suited to handle
Principle
Point: Is the location of the touch points on the method caller's coordinate system-(UIView *) HitTest: (Cgpoint) point withevent: (Uievent *) event{1. Determine if the touch event can be receivedif (Self. userinteractionenabled = =NO | |Self. hidden = =YES | |Self. Alpha <=0.0)ReturnNil2. Determine if the next point is not on the controlif ([Self Pointinside:point withevent:event] = =NO)ReturnNil3. Traverse the child control from the back forwardint count = (intSelf. Subviews. Count; for (int i = count- 1; I >= 0; i--) { //Take out the child control displayed at the front UIView *childview = self . Subvie Ws[i]; //Convert to child control coordinate system on point cgpoint CHILDP = [selfconvertpoint:point toview:childview]; UIView *fitview = [Childview hittest:childp withevent:event]; if (fitview) { return fitview;}} //Indicates there is no more appropriate view return self ;}
The complete process of event delivery
1> first 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.
2> calls the touches of the most appropriate control .... method
3> if [super touches ...] is called, the event is passed up the responder chain to the previous responder
4> then calls the last responder's touches .... method
How to determine the previous responder
1> If the current view is a view of the controller, then the controller is the last responder
2> If the current view is not the controller's view, then the parent control is the last responder
The event delivery process for the responder chain
- If the view controller is present, it is passed to the controller, and if the controller does not exist, it is passed to its parent view
- In the top-most view of the view hierarchy, if you 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, it passes the event or message to the UIApplication object
- If UIApplication cannot process the event or message, it is discarded
Responder Chain
- Responder chain: A chain linked by multiple responder objects
- Role: can clearly see the relationship between each responder, and can let an event multiple object processing.
- Responder object: An object that can handle events
The practice of listening for touch events
- If you want to listen to a touch event on a view, the previous practice is to customize a view
- Implement the touches method of view, implement the specific processing code inside the method
- There are a few obvious drawbacks to monitoring the view touch event through the touches method
- 1. You must customize the view
- Because the touch event is monitored in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view by default
- Not easily differentiate user's specific gesture behavior
Uigesturerecognizer
- To complete gesture recognition, the gesture recognizer----Uigesturerecognizer
- With Uigesturerecognizer, you can easily identify some common gestures that users make on a view
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)
ios-control event handling in response to user control events