Touch Events in iOS

Source: Internet
Author: User

1. Responder Object
Not all objects in iOS can handle events, and only objects that inherit Uiresponder can receive and handle events, which we call "responder objects."
UIApplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are both responder objects that can receive and handle events.

2.UIResponde

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: (uieventsbutype) 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
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;
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;
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

4.UITouch
When the user touches the screen with his finger, a Uitouch object is created that is associated with the finger.
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 be kept in the touch position of the finger.
When the finger leaves the screen, the system destroys the Uitouch object.
Tip: To avoid using double-click events in iphone development!

Properties of 5.UITouch
The window where the touch is generated
@property (Nonatomic, ReadOnly, retain) Uiwindos *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;
Recording when a touch event occurs or changes, the unit is seconds
@property (nonatomic, readonly) nstimeinterval timestamp;
The state of the current touch event
@property (nonatomic, readonly) Uitouchphase phase

Method of 6.UITouch

-(Cgpoint) Locationinview: (UIView *) view;
The return value represents 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 as the origin).
When called, the view parameter is nil, which returns the position of the touch point in the UIWindow.

-(Cgpoint) Previouslocationinvview: (UIView *) view;
This method records the position of the previous touch point

6.UIEvent
generates an Uievent object for each event.
Uievent: Called the event object, which records the time and type at which the event occurs.

Common Properties
Event type
@property (nonatomic, readonly) Uieventtype  type;
@property (nonatomic, readonly) Uieventsubtype  subtype;

Event-generated time
@property (nonatomic, readonly) nstimeinterval  timestamp;

Uievent also provides a response method to get a Touch object (Uitouch) above a view.

8.touche 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; The
has nsset  *touches and uievent  *event two parameters in 4 touch event handling methods.
during a full touch process, only one event object is generated, and 4 touch methods are the same event parameter.
If two fingers touch a view at the same time, the view calls only 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 calls 2 times Touchbegan:withevent: Method, and only one Uitouch object is included in the touches parameter each time it is called. The
can determine whether a single-touch or multi-touch is based on the number of Uitouch in touches.

9. Generation and transmission of time
After the touch time is generated, the event is added to an event queue managed by UIApplication.
UIApplication takes the first event from the event queue and tells the event to be distributed for processing, usually by sending an event to the application's main window (Keywindow);
The main window finds the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process.
Once the appropriate view control is found, the touches method of the view space is called to do the specific event handling
Touchesbegan ...
Touchesmoved ...
Touchedended ...

UIView does not accept three cases of touch events
User interaction is not accepted: userinteractionenabled = no;
Hidden: hidden = YES;
Transparent: Alpha = 0.0~0.01
Hint: Uiimageview's userinteractionenadbled default is no, the hidden Thorn Uiimageview and its child controls cannot receive touch events by default.

10. Detailed process of touch event handling
A touch time that occurs after the user taps the screen, 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.

11. The event delivery process for the responder chain
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
In the top-most view of the view hierarchy, if you cannot process the received event or message, it passes the event or message to the Window object for processing
If the window object is not processed, it passes the event or message to the UIApplication object
If UIApplication cannot process the event or message, it is discarded

12. The practice of listening for touch events
If you want to listen to a touch event on a view, the previous practice is to
Customizing a View
Implement the touches method of view, implement the specific processing code inside the method

There are a few obvious drawbacks to monitoring the view touch event through the touches method
You have to customize the view
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
Not easily differentiate 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.

13.UIGestureRecognizer
In order to complete gesture recognition, the gesture recognizer must be used----Uigesturerecognizer

With Uigesturerecognizer, you can easily identify some 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
UITapGestureRecognizer (percussion)
Uipinchgesturerecognizer (pinch, for zooming)
Uipangesturerecognizer (drag)
Uiswipegesturerecognizer (Swipe)
Uirotationgesturerecognizer (swivel)
Uilongpressgesturerecognizer (Long Press)

14.UITapGestureRecognizer
Each gesture recognizer uses a similar set of steps, such as UITapGestureRecognizer:
To create a gesture recognizer object
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

To set the specific properties of a gesture Recognizer object
2 consecutive strokes
tap.numberoftapsrequired = 2;
It takes 2 fingers to tap together.
tap.numberoftouchesrequired = 2;

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

Triggering of a listening gesture
[Tap addtarget:self Action: @selector (Tapiconview:)];

15. Status of gesture recognition
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
};

Complementary: The complete process of time,
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 you call [super touches ...] method, the event is passed along the responder chain to the previous responder.
4. The last responder's touches is then called ... Method

What is the responder chain:
1. The responder chain is a chain that is linked by multiple responder objects (what is the Responder object: the object that can handle the event).
2. Use the responder chain to allow multiple controls to handle the same touch event.
3. How to use the chain to pass up? See who is the last responder.

Who was the last responder?
1. If the current view is a view of the controller, then the controller is the last responder.
2. If the current view is not the controller's responder, then its parent control is the previous responder.

Touch Events in iOS

Related Article

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.