Touch events, gesture recognition

Source: Internet
Author: User

• Not all objects in iOS can handle events, only objects that inherit Uiresponder can receive and handle events. What we call the "responder object" uiapplication, Uiviewcontroller, and UIView inherit from Uiresponder, so they are all responder objects and are capable of receiving and handling events · Uiresponder internally provides the following methods to handle event O 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 Event

-(void) Remotecontrolreceivedwithevent: (Uievent *) event;

Ø Finger Press ø-(void) touchesbegan:ø finger move ø-(void) touchesmoved:ø finger lift ø-(void) Touchesended:ø unexpected interrupt event (phone interrupt) ø-(void) Touchescancelled: UIView is a subclass of Uiresponder that can override the following 4 methods to handle different touch events o 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 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

Øø one or more fingers out of 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, the system automatically calls the following method of view

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

Tip: Uitouch objects are stored in the touches

* nsset: 1> unordered, non-repetitive. The content stored in Nsset is not sorted and added in the order of the 2> anyobject to access a single element 3> traverse Nsset Each element 4> benefits: High efficiency. 5> Application Scenario: * For example, when reusing a Cell, get one from the cache pool, no need to get the * in the specified order when you need to store the data in a collection and then determine if there is an object in the collection * Nsarray 1> ordered, You can have duplicate objects. The order of the objects is saved in the order in which they are added 2> benefits: Ordered access to the 3> scenario: In the vast majority of situations that require a dependency order (such as a collection of data sources for TableView, to obtain objects in real-world operations based on subscripts) 3> access by subscript-Introduction Uitouch Object • @property (nonatomic,readonly) nsuinteger          tapcount;  //touch within a certain point within a certain amount of time @property (nonatomic,readonly,re tain) uiview      *view;-(Cgpoint) Locationinview: (UIView *) view;-(Cgpoint) Previouslocationinview: (UIView *) view;-case: Follow the mouse walk uiview 1> Customize a UIView add to the interface 2> in the TouchMove method, get the current touch point, let the custom The center of the semantic View equals the current touch point 3>  Perfect: Calculates the current touch point, the previous touch point, and calculates the offset. Set the center point in the UIView to offset or use transform when the user touches the screen with one finger, Creates a Uitouch object associated with the finger • The role of one finger for a Uitouch object Uitouch Ø holds information related to the finger, such as the position of the touch, the time, the orderParagraph

• 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 double-click events in iphone development! • The window where the touch is generated

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

• The view at which the touch is generated

@property (Nonatomic,readonly,retain) UIView *view;

• Click the number of times in a short period of time to determine clicks, double clicks, or more by Tapcount.

@property (nonatomic,readonly) Nsuinteger Tapcount;

• Record the time when a touch event is generated or changed, in seconds

@property (nonatomic,readonly) nstimeinterval timestamp;

• The status of the current touch event

@property (nonatomic,readonly) uitouchphase phase;

-(Cgpoint) Locationinview: (UIView *) view; Ø The return value indicates the position of the touch on the view o 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 the input view parameter is nil when called, it returns the position of the touch point in UIWindow Ø-(CGPOINT) Previouslocationinview: (UIView *) view; Ø This method records the position of the previous touch point

• Each occurrence of an event produces a Uievent object uievent: Called the event object, recording the time and type of event generation • Common Properties O Event type

@property (nonatomic,readonly) uieventtype type;

@property (nonatomic,readonly) Uieventsubtype subtype;

Ø The time the event was generated

@property (nonatomic,readonly) nstimeinterval timestamp;

Uievent also provides a way to get a touch object on a view (Uitouch) • A complete touch process that undergoes 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 event parameter øø if two fingers touch a view at the same time, Then view will only call once Touchesbegan:withevent: method, the 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 the touches parameter of each call contains only one Uitouch object øø based on touches in Uitouch can be judged by the number of single-touch or multi-touch · After a touch event occurs, the system adds the event to a uiapplication-managed event queue 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 will find the most appropriate view in the view hierarchy to handle touch events, which is the first step in the entire event processing process and the main window calls Hittest:withevent: Method finds the most appropriate sub-view in the view inheritance tree to handle the touch event, which is the hit-test view. After finding the appropriate view control, the touches method of the view control is called to make the specific event handling Øtouchesbegan ... Øtouchesmoved ... øtouchedended... Touch events are passed from the parent control to the child controls o click on the Green View:

UIApplication, UIWindow, white-green

Ø Click on the Blue View:

UIApplication, UIWindow, white-orange blue

Ø Click on the Yellow view:

UIApplication, UIWindow, White, orange and blue

• If the parent control cannot receive touch events, then the child controls will not be able to receive touch events (mastering) • How do I find the most appropriate control to handle events? Ø Can I receive touch events? Userinteractionenabled et cetera o touch point is it on its own? Ø Iterate through the child controls backwards, repeating the previous two steps o if there are no child controls that meet the criteria, then you are best suited to handle 1. Do not receive user interaction

userinteractionenabled = NO

2. Hide

Hidden = YES

3. Transparent

Alpha = 0.0 ~ 0.01

4. If the location of the child view is outside the valid range of the parent view, then the child view cannot interact with the user, even if the parent view is set to Clipstobounds = NO, it can be understood, but it is also unable to interact with the user

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

• A touch event generated after the user taps the screen, after some column passes, will find the most appropriate view control to handle the event · • Once the most appropriate view control is found, the control's touches method is called to make the specific event handling Øtouchesbegan ... Øtouchesmoved ... Øtouchedended ... Ø The default approach to these touches methods is to pass the event up the responder chain, handing the event to the previous responder for processing · 1. If the view controller is present, it is passed to the controller, if the controller does not exist, It is passed to its parent view 2. 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 3. If the Window object is not processed, it passes the event or message to the UIApplication object 4. If Uiapplic ation cannot process the event or message, it is discarded • If you want to listen to a touch event on a view, the previous practice is to customize a Viewø implementation view of the touches method, within the method to implement specific processing code Ø through the touches method to listen to the view Touch event, There are obviously a few shortcomings o must be customized viewø because it is the touches method in the view to listen for touch events, so by default, no other external objects to listen to the touch event of the view o not easily distinguish the user's specific gesture behavior iOS 3.2, Apple has introduced the gesture recognition function (Gesture recognizer), which greatly simplifies the developer's difficulty in touch event processing • To complete gesture recognition, the gesture recognizer must be used----Uigesturerecognizer With Uigesturerecognizer, it's easy to identify some common gestures that users make on a view Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures. Use its subclasses to handle a specific gesture Øuitapgesturerecognizer (tap) øuipinchgesturerecognizer (pinch, for zooming) Øuipangesturerecognizer (drag) Øuiswipegesturerecognizer (swipe) øuirotationgesturerecognizer (rotate) Øuilongpressgesturerecognizer (Long Press) • The use of each gesture recognizer is similar , such as UITapGestureRecognizer, use the following steps o 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;

It takes 2 fingers to tap together.

tap.numberoftouchesrequired = 2;

Ø Add gesture recognizer to the corresponding view

[Self.iconview Addgesturerecognizer:tap];

Ø Trigger of the monitor gesture

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

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

};

Touch events, gesture recognition

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.