Touch Event Basic Introduction

Source: Internet
Author: User

Events in iOS

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, UIView inherit? Since Uiresponder, so they are all responder objects that are able to 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 Uiresponder? sub-class, can cover the following 4? Methods to handle different touch events
?? One or more fingers start to touch the view, the system will automatically adjust the view of the next face?
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

????????????????????????????????????????

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

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
? Before the touch is over, a system event (such as a phone call) interrupts the touch process, and the system automatically notes the next face of the view.

? method

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

Touches: The Uitouch object is stored in the

Uitouch

When the user touches the screen with a finger, it creates a Uitouch object associated with the finger? A finger? A Uitouch Object

Uitouch's work?
? Keep information about your 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
In the iphone development, to avoid the use of double-click event!

Properties of the Uitouch

Touch the window where the production is born

@property (Nonatomic,readonly,retain) UIWindow *window;

Touch the view of the production at birth

@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;

Record the time of the touch event birth or change, in seconds

@property (nonatomic,readonly) nstimeinterval timestamp;

The state of the current touch event

@property (nonatomic,readonly) uitouchphase phase;

Uitouch's? method

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

Return value table The position of the touch on the view

This is where the return position is for the view's coordinate system (with the top left corner of view as the origin point (0, 0))

If the view parameter is nil with simultaneous, the position of the touch point in UIWindow is returned.

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

This method records the position of the front? A touch point

Uievent

Every birth? An event will produce a Uievent object
Uievent: Called Event object, records the time and type of event birth

Common Properties
? Event Type
@property (nonatomic,readonly) uieventtype type; @property (nonatomic,readonly) Uieventsubtype subtype;

? The time of the birth of the event
@property (nonatomic,readonly) nstimeinterval timestamp;

Uievent also provides a corresponding method to obtain a touch object on a View face (Uitouch)

Touches and event parameters

? A complete touch process will go 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 have Nsset *touches and uievent *event two parameters
? If two fingers touch a view at the same time, then the view will only be adjusted. Once touchesbegan:withevent:


?? A complete touch process, will only produce? An event object, 4 touch? Methods are all the same. An event parameter

? method, the touches parameter is loaded with 2 Uitouch objects

? What if these two fingers? One after the other to touch the same view, then the view will be adjusted 2 times touchesbegan:withevent:? method, and the time of each tune is only included in the touches parameter? A Uitouch object

? Judging by the number of Uitouch in touches, single-touch or multi-touch.

The birth and delivery of events

After the touch event, the system will add the event into? An event queue managed by UIApplication
UIApplication will remove the most front-facing event from the event queue and distribute the event for processing

Usually, first send the event to the main window of the program?? Port (Keywindow)
The main window?? The port will be found in the view hierarchy? One of the most appropriate views to handle touch events, which is also the entire event handling

The first step of the process

Once the appropriate view control is found, the touches of the view control is used for specific event handling? Touchesbegan ...
? Touchesmoved ...
? Touchedended ...

Event Delivery? example

UIView three cases of not receiving touch events

Do not receive? user interaction userinteractionenabled = no

. Hide hidden = YES

. Transparent
Alpha = 0.0 ~ 0.01

Uiimageview: userinteractionenabled default is no, so Uiimageview and its child controls cannot receive touch events by default

Detailed process of touch event handling

? The user taps the screen after the production? A touch event, passing through? After some columns are passed, the most appropriate view control is found to handle this event

Once the most appropriate view control is found, the touches of the control is used for specific event handling? Touchesbegan ...
? Touchesmoved ...
? Touchedended ...

These touches? The default approach is to pass the event up the responder chain, handing the event over to the top? A responder in-line processing

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. The top-most view of the view hierarchy, and if the received event or message cannot be processed, the event

or message passed to the Window object in-line 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, discard it

The practice of listening for touch events

If you want to listen to a touch event on a view surface, the previous practice is
?? custom? A view
? Implement the touches method of view, implement the specific processing code inside the method.

Listen to the view touch event through the touches method, there are some obvious disadvantages

? You have to? Custom view

? Because the touch event is monitored in the touches method inside the view, by default, there is no way for other outside objects to listen to the touch events of the view

? Not easy to distinguish? User's specific gestures? behavior
After IOS 3.2, Apple launched the gesture recognition feature (Gesture recognizer), in touch event handling

The face of the square, the big large, simplifies the developer's development difficulty

Uigesturerecognizer

To complete? Gesture recognition, you must use the gesture recognizer----Uigesturerecognizer

With Uigesturerecognizer, can you easily identify? What do users do on a view? Some common gestures

Uigesturerecognizer? An abstract class that defines the basic behavior of all gestures, so that it can be used to handle concrete? gestures

? UITapGestureRecognizer (percussion)
? Uipinchgesturerecognizer (pinch,? for zooming)? Uipangesturerecognizer (drag)
? Uiswipegesturerecognizer (Swipe)
? Uirotationgesturerecognizer (swivel)
? Uilongpressgesturerecognizer (Long Press)

UITapGestureRecognizer

Every one? The use of the gesture recognizer is similar, like the UITapGestureRecognizer of the following steps
? Create a gesture Recognizer object
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

? Set the specific properties of the gesture Recognizer object
2 consecutive strokes tap.numberoftapsrequired = 2;//requires 2 fingers? Strike together tap.numberoftouchesrequired = 2;

? Add the gesture recognizer to the corresponding view
[Self.iconview Addgesturerecognizer:tap];

? The trigger of a gesture?
[Tap addtarget:self Action: @selector (Tapiconview:)];

? Status of Gesture recognition

typedef ns_enum (Nsinteger,uigesturerecognizerstate) {

No touch events, no health, all? Default state of gesture recognition

Uigesturerecognizerstatepossible,

? A gesture has started but has not changed or completed

Uigesturerecognizerstatebegan,

? gesture State Change

Uigesturerecognizerstatechanged,

? gesture Completion

Uigesturerecognizerstateended,
? gesture cancellation, recovery? To possible status

Uigesturerecognizerstatecancelled,

? gesture failed, recovery? To possible status

Uigesturerecognizerstatefailed,

? gesture recognition status change?


Touch Event Basic Introduction

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.