Touch events and gestures for iOS development

Source: Internet
Author: User

1. IOS the events in

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:

2 , 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.

2 , Uiresponder

inherited the Uiresponder to handle the event. 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 the touch event handling

UIView is a subclass of Uiresponder that can handle different touch events by overwriting the following 4 methods:

When one or more fingers begin to touch the view, the system automatically calls the following method of view:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

With one or more fingers moving 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

When one or more fingers leave the view, the system automatically calls 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

The Uitouch object is stored in the notes touches. UIView by default, multi-touch is not supported, and the option to make IT support multi-touch is to check the following options:

Various data about the finger movement can be obtained by the above touches parameter, for example, to move the UIView with the finger:

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event

{

Uitouch *touch = [touches anyobject];

Current touch point, set to self-reference, coordinate origin to the upper-left corner of yourself (self)

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;

}

4 , Uitouch

When the user touches the screen with one finger, a Uitouch object is created that is associated with the finger, and 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:

(1) When the finger moves, the system updates the same Uitouch object so that it can keep the finger in the touch position

(2) When the finger leaves the screen, the system destroys the corresponding Uitouch object

"Remarks" in iphone development, to avoid using double-click events!

5 , Uitouch of the Properties

The window at which the touch occurs:

@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 the Tapcount click, double click, or more clicks:

@property (nonatomic,readonly) Nsuinteger Tapcount;

Records the time when a touch event is generated or changed, in seconds:

@property (nonatomic,readonly) nstimeinterval timestamp;

Status of the current touch event:

@property (nonatomic,readonly) uitouchphase phase;

The "Memo" 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)

6 , Uitouch the method

-(Cgpoint) Locationinview: (UIView *) view;

The return value represents the position of the touch on the view, where the position returned is for the view's coordinate system (the origin (0, 0) in the upper-left corner of the view), and the return of the view parameter to nil when called, and returns the position of the touch point at UIWindow.

-(Cgpoint) Previouslocationinview: (UIView *) view;

This method records the position of the previous touch point

7 , uievent

Each occurrence of an event produces a Uievent object.

uievent :

Called the event object, which records 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;

The Notes Uievent also provides a way to get a Touch object (Uitouch) on top of a view.

typedef ns_enum (Nsinteger, Uieventtype) {

Uieventtypetouches,

Uieventtypemotion,

Uieventtyperemotecontrol,

};

typedef ns_enum (Nsinteger, Uieventsubtype) {

Available in IPhone OS 3.0

Uieventsubtypenone = 0,

For Uieventtypemotion, available in IPhone OS 3.0

Uieventsubtypemotionshake = 1,

For Uieventtyperemotecontrol, available in IOS 4.0

Uieventsubtyperemotecontrolplay = 100,

Uieventsubtyperemotecontrolpause = 101,

Uieventsubtyperemotecontrolstop = 102,

Uieventsubtyperemotecontroltoggleplaypause = 103,

Uieventsubtyperemotecontrolnexttrack = 104,

Uieventsubtyperemotecontrolprevioustrack = 105,

Uieventsubtyperemotecontrolbeginseekingbackward = 106,

Uieventsubtyperemotecontrolendseekingbackward = 107,

Uieventsubtyperemotecontrolbeginseekingforward = 108,

Uieventsubtyperemotecontrolendseekingforward = 109,

};

8 , touches and the 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

Of the 4 touch event handling methods, there are Nsset *touches and uievent *event two parameters:

(1) During a complete touch process, only one event object is generated, and 4 touch methods are the same as event parameters

(2) 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

(3) 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

(4) According to the number of uitouch in touches can be judged whether it is a single touch or multi-touch

"Remarks" typedef ns_enum (Nsinteger, Uieventtype) {

Uieventtypetouches,

Uieventtypemotion,

Uieventtyperemotecontrol,

};

typedef ns_enum (Nsinteger, Uieventsubtype) {

Available in IPhone OS 3.0

Uieventsubtypenone = 0,

For Uieventtypemotion, available in IPhone OS 3.0

Uieventsubtypemotionshake = 1,

For Uieventtyperemotecontrol, available in IOS 4.0

Uieventsubtyperemotecontrolplay = 100,

Uieventsubtyperemotecontrolpause = 101,

Uieventsubtyperemotecontrolstop = 102,

Uieventsubtyperemotecontroltoggleplaypause = 103,

Uieventsubtyperemotecontrolnexttrack = 104,

Uieventsubtyperemotecontrolprevioustrack = 105,

Uieventsubtyperemotecontrolbeginseekingbackward = 106,

Uieventsubtyperemotecontrolendseekingbackward = 107,

Uieventsubtyperemotecontrolbeginseekingforward = 108,

Uieventsubtyperemotecontrolendseekingforward = 109,

};

9 , the generation and delivery of events

After a touch event occurs, the system adds the event to an event queue managed by UIApplication, uiapplication the first event from the event queue and distributes the event for processing, typically Sending an event to the application's main window (Keywindow), the main window finds the most appropriate view in the view hierarchy to handle the touch event, which is also the first step in the entire event processing process, after finding the appropriate view control The touches method of the view control is called to make specific event handling:

Touchesbegan ...

Touchesmoved ...

Touchedended ...

Event Delivery Example:

The passing of a touch event is passed from the parent control to the child control:

(1) Click on the Green View:

UIApplication, UIWindow, white-green

(2) Click on the Blue View:

UIApplication, UIWindow, white-orange blue

(3) Click on the Yellow view:

UIApplication, UIWindow, White, orange and blue

Note: If the parent control cannot receive touch events, the child controls will not be able to receive touch events ( mastering ).

Ten , UIView three cases of not receiving touch events

(1) Do not receive user interaction

userinteractionenabled = NO

(2) Hide

Hidden = YES

(3) Transparent

Alpha = 0.0 ~ 0.01

The userinteractionenabled default for Memo Uiimageview is no, so Uiimageview and its child controls cannot receive touch events by default.

11. detailed process of touch event handling

After the user taps the screen, a touch event occurs, and after some column passes, the most appropriate view control is found to handle the event. Once the most appropriate view control is found, the control's touches method is called to do the specific event handling:

Touchesbegan ...

Touchesmoved ...

Touchedended ...

The default practice of these touches methods is to pass the event up the responder chain, handing the event over to the previous responder for processing.

A , responder chain

11. the event delivery process for the responder chain

(1) If the view controller is present, it is passed to the controller, and if the controller does not exist, it is passed to its parent view.

(2) in the top-most view of the view hierarchy, if the received event or message cannot be processed, it passes the event or message to the window object for processing.

(3) If the Window object is not processed, it passes the event or message to the UIApplication object.

(4) If uiapplication cannot process the event or message, it is discarded.

- , Touch event complete Process

1. First pass the event object from top to bottom (passed by the parent control to the child control), and find the most appropriate control to handle the event.

2. Call the touches of the most appropriate control ... Method.

3. If the control calls the [super touches ...]; The event is passed down the corresponding chain and passed to the next responder.

4, then will call the next responder touches ... Method.

5, the event can also continue to pass down until uiapplication, if the uiapplication also do not process the event or message, it is discarded.

"Remarks" about the next responder above:

(1) If the current view is the view of the controller, then the controller is the next responder.

(2) If the current view is not the controller's view, then the parent control is the next responder.

- , Uigesturerecognizer

If you want to listen to a touch event on a view, the previous practice is:

(1) Customize a view.

(2) Implement the touches method of view, implement the specific processing code inside the method.

There are a few obvious drawbacks to monitoring a view touch event through the touches method:

(1) You have to customize the view.

(2) Because the touch event is monitored in the touches method inside the view, it is not possible to allow other external objects to listen to the touch events of the view by default.

(3) It is not easy to distinguish user's specific gesture behavior.

After IOS 3.2, Apple introduced the gesture recognition feature (Gesture recognizer), which greatly simplifies developer development in touch event handling.

In order to complete gesture recognition, it is necessary to use the gesture recognizer----Uigesturerecognizer, using Uigesturerecognizer to easily identify some of the common gestures that users make on a view, Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures and uses its subclasses to handle specific gestures:

(1) UITapGestureRecognizer (percussion)

(2) Uipinchgesturerecognizer (pinch, for zooming)

(3) Uipangesturerecognizer (drag)

(4) Uiswipegesturerecognizer (swipe)

(5) Uirotationgesturerecognizer (rotation)

(6) Uilongpressgesturerecognizer (Long Press)

the , UITapGestureRecognizer

1 , how to use

Each gesture recognizer uses a similar set of steps, such as the following uitapgesturerecognizer:

Check the following options first:

First step: Create a Gesture Recognizer object

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

Step two: Set the specific properties of the gesture Recognizer object (the default is to tap once)

2 consecutive strokes

tap.numberoftapsrequired = 2;

It takes 2 fingers to tap together.

tap.numberoftouchesrequired = 2;

Step Three: Add the gesture recognizer to the corresponding View on

[Self.iconview Addgesturerecognizer:tap];

Fourth step: The trigger of the listening gesture

[Tap addtarget:self Action: @selector (Tapiconview:)];

Event occurrence invokes the custom Tapiconview method.

Note: You can get the view that was clicked through Tap.view.

2, the agent of UITapGestureRecognizer

Set the agent for UITapGestureRecognizer to be the controller that the UIView is located on, abide by the agent protocol uitapgesturerecognizerdelegate. You can then implement the appropriate proxy methods, such as:

/**

* When clicking on the view, this method is called first, and no is intercepted.

* Click events

*/

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch

{

So that only the left half of the response, click on the right no response

Cgpoint pos = [Touch LocationInView:touch.view];

if (pos.x <= self.iconView.frame.size.width * 0.5) {

return YES;

}

return NO;

}

- , gesture recognition status

typedef ns_enum (Nsinteger, uigesturerecognizerstate) {

No touch event occurs, default state of all gesture recognition

Uigesturerecognizerstatepossible,

When a gesture has started but has not changed or completed

Uigesturerecognizerstatebegan,

Gesture State Change

Uigesturerecognizerstatechanged,

Gesture Completion

Uigesturerecognizerstateended,

Gesture cancellation, revert to possible state

Uigesturerecognizerstatecancelled,

Gesture failed to revert to possible state

Uigesturerecognizerstatefailed,

Recognition to gesture recognition

uigesturerecognizerstaterecognized = uigesturerecognizerstateended

};

Gesture Recognition status change:

Touch events and gestures for iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.