5, touch events



Representing a touch event (starting from the first finger, touching the screen to the last finger, leaving the screen), an UIevent consists of multiple UITouch objects, with several fingers having several touch objects.

Property: UIEventType UIEventTypeTouches / / UIEventTypeMotion / /, touch, mobile UIEventTypeRemoteControl, NS_ENUM_AVAILABLE_IOS / UIEventTypePresses remote control (9_0), the strength of the press / / attributes: allTouchs, represents a collection of all UITouch objects


A click event indicating the root of a finger indicating the position, touch, movement, and exit of the current finger

- (CGPoint) locationInView: (nullable * UIView) view; / / get the current click in view coordinates (CGPoint) previousLocationInView: (nullable UIView *) view// to get a click point in view coordinates


In iOS, objects that correspond to events are UIResponder subclass objects. UIResponder provides four methods for the user to click on the callback (start, move, end, cancel), calling the cancel event only when the program is forced to exit and the caller is unexpectedly interrupted.

- (void) touchesBegan: (NSSet< UITouch *> touches withEvent: (*) nullable * event; UIEvent) - (void) touchesMoved: (NSSet< UITouch *> touches withEvent: (*) nullable * event; UIEvent) - (void) touchesEnded: (NSSet& lt; UITouch *> touches withEvent: nullable UIEvent (* *) event - (void)); touchesCancelled: (NSSet< UITouch *> touches withEvent: (*) nullable * event; UIEvent) - (void) touchesEstimatedPropertiesUpdated: (NSSet< UITouch *> touches NS_AVAILABLE_IOS (9_1) *);

You can override the above method for click callback monitoring in UIResponder.

When multiple views are superimposed and gestures conflict with each other, how do you decide which View handles which time?

The generation and processing of response chains are now involved.

Response chain

The response chain is composed of UIResponder of the current view, all of which can perform or respond to touch events, and is a tree structure

  • Formation of response chains: appdelegate-> UIApplication–> UIWindow–> RootViewController–> RootViewController.view……
  • In response to the incident distribution, from the beginning of the first UIWindow objects, first determine whether UIWindow is qualified, then click on the judgment in a position within this Window, if not, return to the nil, for the next UIWindow; if so, UIWindow and subView did not return to their own, the end of the process. If UIWindow has subViews, go through the entire subViews from the back, and do something similar to UIWindow until you find a View. If you don’t find it, you don’t do it. (qualified) means: the control allows the receive time, 1, cannot be hidden, the value of 2, alpha is greater than 0.013, isUserInterface is YES, and UILabel and UIImageView default is NO)
  • In response to the judgment of the event, by distributing the view found, call the – (nullable, UIView *), hitTest: (CGPoint), point, withEvent: (nullable, UIEvent *) event to determine whether which view is processed.
Simulation system of judge - (nullable / / UIView *) hitTest: (CGPoint) point withEvent: (nullable * UIEvent) event {/ / to determine whether qualified if (self.hidden & self.alpha! & > & 0.01; & self.isUserInteractionEnabled) {/ / judge click position is in the if region ([self pointInside: point internal withEvent:event]) {UIView *attachedView; for (int i = self.subviews.count - 1; I = 0; > i--) {UIView *view = self.subviews[i]; hitTest = attachedView / view on [view hitTest:point withEvent:event] (attachedView); if break;} if {return attachedView (attachedView); } else {return self;}} returnnil}

Better article http://www.jianshu.com/p/cb0314b72883