IOS (Supplemental) touch event processing
[1] basic concepts of events
UIEvent: an event is an object captured by the hardware that represents the user's operation on the device.
There are three categories: Touch events, shaking events, and remote control events.
Touch event: You can touch the device screen to manipulate objects and input data. Supports multi-touch, including one or more touch points
UIView supports touch events (because it inherits from UIResponder) and supports multi-point touch.
You need to define the UIView subclass to implement touch-related methods.
Touches .. began,
Touches... moved,
Touches... ended,
Touches... canceled
[2] gesture: regular touch.
UITouch represents a finger that is touched on the screen. You can obtain the touch time and position.
How to obtain the touch object. The touches set contains all? Potential
What is a responder chain
The responder chain is a chain composed of multiple responder objects.
Basic concepts of events
UIEvent: an event is an object captured by the hardware that represents the user's operation on the device.
There are three categories: Touch events, shaking events, and remote control events.
Touch event: You can touch the device screen to manipulate objects and input data. Supports multi-touch, including one or more touch points
UIView supports touch events (because it inherits from UIResponder) and supports multi-point touch.
You need to define the UIView subclass to implement touch-related methods.
Touches .. began,
Touches... moved,
Touches... ended,
Touches... canceled
Gesture: regular touch.
UITouch represents a finger that is touched on the screen. You can obtain the touch time and position.
How to obtain the touch object. The touches set contains all? Potential
[3] What is a responder chain
The responder chain is a chain composed of multiple responder objects.
UIResponder. Response class.
All objects in iOS that can respond to events (touch, shake, and remote events) are responder.
The system defines an abstract parent class UIResponder to indicate the responder. Its subclass is the responder.
After the hardware detects a touch operation, it submits the information to the UIApplication to start detection.
UIApplication-> window-> viewController-> view-> detect all? Subview
Finally confirm the Touch Location to complete the Query Process of the responder chain
After detecting the responder, implement methods such as touchesBegan: withEvent: to process the event.
If the responder does not process the event, the event will be passed down. If no responder handles the event, the touch event is discarded.
The event processing sequence is opposite to the touch detection query.
Touch? View> viewController> window> UIApplication
The responder chain can be interrupted .? Method to complete the detection and query process.
View class attribute: userInteractionEnabled. The query process can be blocked.
Code:
# Import "TestView. h "# import" RootView. h "# define KRandomColor arc4random () % 256/255. 0 @ interface TestView () {// CGPoint _ start;} @ end @ implementation TestView-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {self. backgroundColor = [UIColor redColor];} return self;} // when you start to touch an event, execute the predefined execution Event code in touch (when you start to touch, check it here) // when a touch event occurs, this method only runs once-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// random color during touch (KRandomColor is a random number defined in the extension) self. backgroundColor = [UIColor colorWithRed: KRandomColor green: KRandomColor blue: KRandomColor alpha: 1]; // coordinates of the first touch _ start = [[touches anyObject] locationInView: self]; NSLog (@ "");} // If a touch event has not ended, this method will be called all the time. // if it is not finished, it will be touched-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {// The moving vertex CGPoint nowPoint = [[touches anyObject] locationInView: self]; // move the vertex minus the start vertex CGFloat x = nowPoint. x-_ start. x; CGFloat y = nowPoint. y-_ start. y; CGPoint centerPoint = CGPointMake (self. center. x + x, self. center. y + y); self. center = centerPoint; // print the NSLog coordinates when moving (@ "% @", NSStringFromCGPoint (nowPoint);} // The end of a touch time, execute this method // touch done-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "ended ");} // The touch time is interrupted by others, // some people disturb-(void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event {}@ end