Events in iOS
- A variety of events occur during the user's use of the app
- Events in iOS can be divided into 3 major types
--Touch Event ———— Accelerometer Event ———— Remote control event-
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
- (void) touchesbegan: (nsset *) touches withevent: ( Uievent *) event;- (void) touchesmoved: ( Span class= "hljs-variable" >nsset *) touches withevent: (uievent *) event;- Touchesended: (nsset *) touches withevent: ( Span class= "hljs-variable" >uievent *) event;- (void) Touchescancelled: (nsset *) touches withEvent: uievent *) event;
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
UIView Touch Event Handling
UIView is a subclass of Uiresponder that implements the following 4 methods 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 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
The default uiview is to handle one finger, and to handle multiple fingers, you need to turn on multi-touch (multiple touch).
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 hold the finger's touch position all the time.
- When the finger leaves the screen, the system destroys the corresponding Uitouch object
Tip: To avoid using double-click events in iphone development!
Properties of the Uitouch
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;
Uitouchphase is an enumeration type that contains:
Uitouchphasebegan (Touch start)
uitouchphasemoved (Contact point Movement)
Uitouchphasestationary (Contact point no movement)
Uitouchphaseended (Touch end)
Uitouchphasecancelled (Touch Cancel)
Methods of Uitouch
-(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)
typedef ns_enum (Nsinteger, Uieventtype) {uieventtypetouches, uieventtypemotion, UieventtypeRemotecontrol,};typedef Ns_enum (Nsinteger, Uieventsubtype) {//AvailableinchIPhone OS3.0Uieventsubtypenone =0, // forUieventtypemotion, availableinchIPhone OS3.0Uieventsubtypemotionshake =1, // forUieventtypeRemotecontrol, available in IOS 4.0UieventsubtypeRemotecontrolplay =.Uieventsubtyperemotecontrolpause = 101,Uieventsubtyperemotecontrolstop = 102,Uieventsubtyperemotecontroltoggleplaypause = 103,Uieventsubtyperemotecontrolnexttrack = 104,UieventsubtypeRemotecontrolprevioustrack = TheUieventsubtypeRemotecontrolbeginseekingbackward = 106,Uieventsubtyperemotecontrolendseekingbackward = 107,UieventsubtypeRemotecontrolbeginseekingforward = 108,UieventsubtypeRemotecontrolendseekingforward = 109,};
Touches and event parameters
A complete touch process that goes through 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.
Events in iOS