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
Touch events:
Accelerometer events:
Remote Control Events:
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 events
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
UIView is a subclass of Uiresponder that can handle different touch events with the following 4 methods
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 moving on the view, the system automatically calls the view's following method (which continues to be called as the finger moves)
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
One or more fingers to leave the view, the system will automatically call the following method of view
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
A system event, such as a phone call, interrupts the touch process before the touch is finished, and the system automatically calls 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 can be judged by Tapcount click, double tap or more
@property (nonatomic,readonly) Nsuinteger Tapcount;
Records the time when a touch event is generated or changed, in seconds
@property (nonatomic,readonly) nstimeinterval timestamp;
The state of the current touch event
@property (nonatomic,readonly) uitouchphase phase;
Uitouch method
-(Cgpoint) Locationinview: (UIView *) view;
The return value indicates the position of the touch on the view
The position returned here is for the view's coordinate system (in the upper left corner of the view is the origin (0, 0))
When called, the view parameter is nil, which returns the position of the touch point in the UIWindow.
-(Cgpoint) Previouslocationinview: (UIView *) view;
This method records the position of the previous touch point
Uievent
Each occurrence of an event produces a Uievent object
Uievent: Called an event object to record the time and type of event generation
Common Properties
Event Type
@property (nonatomic,readonly) uieventtype type;
@property (nonatomic,readonly) Uieventsubtype subtype;
Time the event was generated
@property (nonatomic,readonly) nstimeinterval timestamp;
Uievent also provides a way to get a touch object on a view (Uitouch)
Touches and event parameters
iOS training organizations analyze a complete touch process and experience 3 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 *) event
4 Touch Event handling methods, all with Nsset *touches and uievent *event two parameters
Once a complete touch process, only one event object is generated, and 4 touch methods are the same as the same event parameter
If two fingers touch a view at the same time, 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 only one Uitouch object is included in the touches parameter of each call
Depending on the number of Uitouch in the touches, you can tell whether it's a single touch or multi-touch.
Touch Event Instances
#import "TouchView.h"
@implementation TouchView
A complete touch process: Touchesbegan, touchesmoved, touchesended
/**
* Touch starts (finger just touches view)
*/
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Event.type
Random fetch of an object
Uitouch *touch = [touches anyobject];
NSLog (@ "touchesbegan--%d", Touches.count);
NSLog (@ "%d", touch.tapcount);
}
/**
* Touch ing (fingers move around the view)
*/
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch *touch = [touches anyobject];
Current Touch Point
Cgpoint current = [Touch locationinview:self];
Previous Touch Point
Cgpoint 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 left view)
*/
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch *touch = [touches anyobject];
NSLog (@ "touchesended--%d", Touches.count);
}
@end
}
@end
iOS Development responder Chain Touch Event