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
UiresponderThe following methods are provided internally 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 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
UIView Touch Event Handling
UIViewIsUiresponderSub-class that can overwrite the following4A method to handle different touch events one or more fingers start to touch the view, the system will automatically call 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 be called as the finger moves)-(void)Touchesmoved:(nsset *)Touches withevent:(uievent *)Event one or more fingers out of view, the system will automatically call the following method of view-(void)Touchesended:(nsset *)Touches withevent:(uievent *)Before the event touch is finished, a system event(for example, phone call-in)Will interrupt the touch process, the system will automatically invoke the following method of view-(void)Touchescancelled:(nsset *)Touches withevent:(uievent *)Event Tip: Uitouch objects are stored in the touches
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 at which the touch was generated@property(nonatomic,ReadOnly, retain)UIView*view, the number of times you tap the screen in a short time, you can judge by Tapcount click, double tap or more@property(nonatomic,ReadOnlyNsuinteger Tapcount; Records the time when a touch event is generated or changed, in seconds@property(nonatomic,ReadOnly)Nstimeintervaltimestamp; the state of the current touch event@property(nonatomic,ReadOnly) Uitouchphase phase;
Uitouch method
- (CGPoint)locationInView:(UIView *)view;返回值表示触摸在view上的位置这里返回的位置是针对view的坐标系的(以view的左上角为原点(00))调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置- (CGPoint)previousLocationInView:(UIView *)view;该方法记录了前一个触摸点的位置
Uievent
每产生一个事件,就会产生一个UIEvent对象UIEvent:称为事件对象,记录事件产生的时刻和类型常见属性事件类型@property(nonatomic,readonly) UIEventType type;@property(nonatomic,readonly) UIEventSubtype subtype;事件产生的时间@property(nonatomic,readonlyNSTimeInterval timestamp;UIEvent还提供了相应的方法可以获得在某个view上面的触摸对象(UITouch)
Touches and event parameters
A complete touch process that goes through3Status: Touch Start:-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)EventTouch Move:-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)EventTouch End:-(void) touchesended: (Nsset *) touches withevent: (Uievent *)EventTouch Cancel (May experience):-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event4Nsset *touches and Uievent * In a touch event handling methodEventTwo parameters a complete touch process produces only one event object,4Each touch method is the sameEventParameter if two fingers touch a view at the same time, view will only call once Touchesbegan:withevent: method, touches parameter is loaded with2A Uitouch object if these two fingers touch the same view one after the other, the view will be called separately2Touchesbegan:withevent: Method, and the touches parameter of each call contains only one Uitouch object based on the number of Uitouch in the touches can be judged whether it is a single touch or a multi-point touch
Touch Event Instances
#Import "TouchView.h"@implementationTouchView//A complete touch process: Touchesbegan, touchesmoved, touchesended/** * Touch Start (finger just touches view) */- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//Event.type //random fetch of an objectUitouch *touch = [touches anyobject]; NSLog (@"touchesbegan--%d", Touches.count); NSLog (@"%d", Touch.tapcount);}/** * Touch ing (fingers move over view) */- (void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{Uitouch *touch = [touches anyobject];//Current touch pointCgpoint current = [Touch locationinview:self];//Previous Touch pointCgpoint previous = [Touch previouslocationinview:self];//Modify the position of the current view (midpoint)Cgpoint Center = self.center; Center.x + = current.x-previous.x; Center.y + = Current.y-previous.y; Self.center = center;}/** * Touch End (finger off view) */- (void) touchesended: (Nsset *) touches withevent: (uievent *) event{//Uitouch *touch = [touches anyobject];//NSLog (@ "touchesended--%d", touches.count);}@end
iOS Development-responder chain touch events