Touch events
A variety of events occur during the user's use of the app
Events in iOS can be divided into 3 major types
触摸事件:加速计事件:远程控制事件:
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
Uiresponder internally provides the following methods to handle event 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 Event- (void) Motionbegan: (uieventsubtype) motion Withevent: (uievent *) event;- (void) Motionended: (uieventsubtype) motion Withevent: ( Span class= "hljs-variable" >uievent *) event;- (void) motioncancelled: (uieventsubtype) motion withevent: uievent *) event; Remote control events- (void) Remotecontrolreceivedwithevent:UIEvent *) event;
UIView Touch Event Handling
UIView isA subclass of Uiresponder that can overwrite the following4 ways to handle different touch events one or more fingers begin to touch the view, and the system automatically calls the following method of view-(void) Touchesbegan:(Nsset *) touches withevent:(uievent *) event One or more fingers are moved on the view, the system automatically calls the following method of view (which continues to call the method as the finger moves)- (void) touchesmoved:( Nsset *) touches withevent:(uievent *) event one or more fingers leave the view, the system automatically calls the following method of view- (void) touchesended:( Nsset *) touches withevent:(uievent *) before the event touch ends, a system event (such as a phone call) interrupts the touch process and the system automatically calls the following method of view- (void) Touchescancelled:(nsset *) touches withevent:(uievent *) event hint: Touches objects are all stored Uitouch
Uitouch
When the user touches the screen with one finger, a Uitouch object associated with the finger is created
One finger corresponds to a Uitouch object
The role of Uitouch
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
Tip: To avoid using double-click events in iphone development!
Uitouch Property
The window where the touch is generated @property (nonatomic,readonly, Retain) uiwindow *window; The view where the touch was generated @property (nonatomic,readonly,retain" UIView *view; The number of times you tap the screen in a short time can be judged by Tapcount click, double-click, or more click @property (nonatomic , readonly) Nsuinteger Tapcount, recording the time when the touch event occurred or changed, in seconds @ Property (nonatomic,readonly) Nstimeinterval timestamp; the status of the current touch event @property (nonatomic,< Span class= "Hljs-keyword" >readonly) uitouchphase phase;
Uitouch method
- (CGPoint)locationInView:(UIView *)view;返回值表示触摸在view上的位置这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置- (CGPoint)previousLocationInView:(UIView *)view;该方法记录了前一个触摸点的位置
Uievent
每产生一个事件,就会产生一个UIEvent对象UIEvent:称为事件对象,记录事件产生的时刻和类型常见属性事件类型@property(nonatomic,readonly) UIEventType type;@property(nonatomic,readonly) UIEventSubtype subtype;事件产生的时间@property(nonatomic,readonly) NSTimeInterval timestamp;UIEvent还提供了相应的方法可以获得在某个view上面的触摸对象(UITouch)
Touches and event parameters
A complete touch process that goes through3 states: Touch Start:-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event Touch Move:-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) Event Touch End:-(void) touchesended: (Nsset *) touches withevent: (Uievent *) Event Touch Cancel (may experience):-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) Events 4 Touch Event handling methods, there are Nsset *touches and uievent * Eventtwo parameters once a complete touch process, only one event object is generated, and4 touch methods are the same Event parameter if two fingers touch a view at the same time, then view will only call once Touchesbegan:withevent: method, touches parameter is loaded with 2 Uitouch Objects If these two fingers touch the same view one after the other, then view will call 2 times touchesbegan:withevent: Method, And the touches parameter in each invocation contains only one Uitouch object, based on the number of Uitouch in touches, can be judged by a single touch or multi-touch .
Touch Event Instances
#Import"TouchView.h"@implementation TouchViewA complete touch process: Touchesbegan, touchesmoved, touchesended/** * Touch starts (finger just touches view) */-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{Event.typeRandomly take an object uitouch *touch = [touches anyobject]; NSLog (@"touchesbegan--%d", Touches.count); NSLog (@ "%d", Touch.tapcount);} /** * touch ing (move your finger over the view) */-(void) touchesmoved: (Nsset *) Touches withevent: (uievent *) event{Uitouch *touch = [touches anyobject]; //current touch point cgpoint present = [Touch locationinview:self]; //previous touch point cgpoint previous = [Touch previouslocationinview:self]; //Modify the current view's position (midpoint) Cgpoint center = self.center; center.x + = current.x-previous.x; Center.y + = Current.y-previous.y; Self.center = center;} /** * touch End (finger leave view) */-(void) touchesended: (Nsset *) touches Withevent: (uievent *) Event{//uitouch *touch = [touches Anyobject]; NSLog (@ "touchesended--%d", Touches.count);} @end
Responder chain Touch events