iOS development UI Chapter-Brief Introduction to event handling
First, the event handling simple introduction
Description: An event in iOS
There are a variety of events that occur during the user's use of the app, and the events in iOS can be divided into 3 major types :
1. Responder Object
Not all objects in iOS can handle events,only objects that inherit Uiresponder can receive and handle events。 What we call "Responder objects" uiapplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are all responder objects that can receive and handle events
2.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;
3.UIView Touch Event Handling
UIView is a subclass of Uiresponder that can handle different touch events with the following 4 methods
(1) 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
(2) 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
(3) One or more fingers leave the view, the system will automatically call the following method of view
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
(4) Before the end of the touch, a system event (such as phone call) interrupts the touch process, 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
4.UITouch
When a user touches a screen, a Uitouch object associated with the finger is created
One finger corresponds to a Uitouch object
(1) 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!
(2) Properties of the Uitouch
The window in which the touch was 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 click or more click @property (nonatomic,readonly) Nsuinteger Tapcount;
Records the time when the touch event was generated or changed, in seconds @property (nonatomic,readonly) nstimeinterval timestamp;
The state of the current touch event @property (nonatomic,readonly) uitouchphase phase;
(3) Methods of Uitouch
1)-(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.
2)-(Cgpoint) Previouslocationinview: (UIView *) view;
This method records the position of the previous touch point
5.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)
6.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.
Second, code example
Code Description: The program uses event processing features to complete a simple small function, click on the view on the interface, so that the view can be moved with the mouse (finger) to drag.
Code:
9 #import "YYview.h"Ten One @implementationYyview A - //Touch Event Full Call procedure Touchesbegan-touchesmoved-touchesended - //called when the finger touches the view the-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { -NSLog (@"Finger Touch View"); - } + - //call when the finger moves +-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event A { atNSLog (@"Finger Movement"); - //Get Uitouch Object -Uitouch *touch=[touches anyobject]; - - //get finger last position -Cgpoint prepoint=[Touch previouslocationinview:self]; in //Print location Information view -NSLog (@"prepoint=%@", Nsstringfromcgpoint (Prepoint)); to + //get the current position of the finger -Cgpoint currentpoint=[Touch locationinview:self]; the //Print location Information view *NSLog (@"currentpoint=%@", Nsstringfromcgpoint (Currentpoint)); $ Panax Notoginseng //calculate the distance the finger moves -CGFloat movex=currentpoint.x-Prepoint.x; theCGFloat movey=currentpoint.y-Prepoint.y; + A //set the x and Y values of the view the //Note: You cannot directly change a property value of an attribute's internal structure +Cgpoint temp=Self.center; -temp.x+=MoveX; $temp.y+=Movey; $Self.center=temp; - - } the - //call when your finger is goneWuyi-(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event the { -NSLog (@"Finger away"); Wu } - About //call when a touch event is interrupted (such as a call) $-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event - { - - } A @end
Program:
Partial print Effect:
Additional notes:
1 //Uitouch *touch = [touches anyobject];2 //get the position of finger touch3 //if Locationinview passes self, the future gets out of its own upper-left corner as the origin (XX) .4 //If the parent view is passed, the future gets it in the upper-left corner of the parent view as the origin (XX) .5 //cgpoint point = [Touch LocationInView:self.superview];6 //NSLog (@ "Touchesbegan%@", Nsstringfromcgpoint (point));7 8 //get the number of finger clicks9 //NSLog (@ "Tapcount =%d", touch.tapcount);
iOS Development UI chapter-Event Handling Brief Introduction 1