IOS learning principles of event processing and ios event principles
In iOS event processing, I have introduced event processing in detail. Here I will describe how it works.
1. UITouch object
There is a set of UITouch objects in the processing method of touch events. What is the purpose of this parameter?
(1) Introduction to UITouch objects
When you touch the screen with a finger, a UITouch object associated with the finger is created.
One finger corresponds to a UITouch object
Role of UITouch
Stores information related to the root finger, such as the position, time, and phase of the touch.
When the finger moves, the system updates the same UITouch object so that it can maintain the touch position of the finger.
When the finger leaves the screen, the system will destroy the corresponding UITouch object.
Tip: iPhone development is in progress. Avoid Double-click events!
(2) UITouch attributes
- Touch the window generated when it is generated
@property (nonatomic, readonly, retain) UIWindow *window;
- View generated when a touch occurs
@property (nonatomic, readonly, retain) UIView *view;
- In a short period of time, you can click, double-click, or more clicks Based on tapCount.
@property (nonatomic, readonly) NSUInteger tapCount;
- The time when a touch event is generated or changed, measured in seconds.
@property (nonatomic, readonly) NSTimeInterval timestamp;
- Status of the current touch event
@property (nonatomic, readonly) UITouchPhase phase;
(3) UITouch Method
// Return value indicates the position of the touch on the View-(CGPoint) locationInView :( UIView *) view; // This method records the position of the previous touch point-(CGPoint) previuslocationinview :( UIView *) view;
The position returned in method 1 is the coordinate system of the View (starting from the upper left corner of the View (0, 0 ))
If the input View parameter is nil when method 1 is called, the returned value is the position of the touch point in the UIWindow.
Instance code:
// When the finger moves on the view-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {NSLog (@ "% s ", __func _); // obtain the UITouch object UITouch * touch = [touches anyObject]; // obtain the current vertex CGPoint curP = [touch locationInView: self]; // obtain the last vertex CGPoint preP = [touch previuslocationinview: self]; // obtain the X axis offset CGFloat offsetX = curP. x-preP. x; // obtain the Y axis offset CGFloat offsetY = curP. y-preP. y; // modify the view position (frame, center, transform) self. transform = CGAffineTransformTranslate (self. transform, offsetX, offsetY); // self. transform = CGAffineTransformMakeTranslation (offsetX, 0 );}
By default, Multiple fingers are not supported. to modify the Multiple Touch attribute, you can hook this attribute on the storyboard.
2. Principle (1) generation and transmission of events
After a touch event occurs, the system adds the event to an event queue managed by UIApplication.
UIApplication extracts the first event from the event queue and distributes the event for processing. Generally, it sends the event to the application's main window (keyWindow) first)
The main window will find the most suitable view in the view hierarchy to process touch events, which is also the first step in the entire event processing process.
After a proper view control is found, the touches method of the View control is called for specific event processing.
TouchesBegan...
TouchesMoved...
TouchesEnded...
(2) Three cases where UIView does not receive touch events
userInteractionEnabled = NO;
hidden = YES;
alpha = 0.0 ~ 0.1
(3) how to find the most suitable control
- Determine whether you can receive touch events. The underlying implementation method is hitTest: withEvent:
// Function: it is used to find the most suitable view // when to call: when an event is passed to the control, this method of the control will be called, find the most appropriate view // point: the current touch point. The Coordinate System of the point is the method caller-(UIView *) hitTest :( CGPoint) point withEvent :( UIEvent *) event {// call the system method to find the most appropriate view and return the most suitable view UIView * fitView = [super hitTest: point withEvent: event]; return fitView ;}
The return value of this method is the view control of event processing.
When this method is rewritten in the UIWindow class, the self. subviews [0] is returned to make the bottom-white view no matter where the vertex is.
Determine whether the touch point is on your own. The underlying implementation method is pointInside: withEvent:
// Function: determine whether the current point is not on the method caller (Control) // call-(BOOL) pointInside :( CGPoint) when determining whether it is the most suitable control) point withEvent :( UIEvent *) event {return YES ;}
- Traverse the child control from the back to the front and repeat the previous two steps.