The gesture recognition _ios of IOS development

Source: Internet
Author: User

First, Uigesturerecognizer simple introduction

We have learned to touch event processing, but touch event processing is cumbersome, each touch event processing needs to implement 3 touches methods, more cumbersome, in fact, we can use a more simple touch event processing operations, that is gesture recognition uigesturerecognizer.

Gesture recognition Operation base class Uigesturerecognizer is not directly used, we often use its subclasses, these gesture operation classes are inherited from the Uigesturerecognizer class

Common gesture Recognition Subclass:

Click the gesture UITapGestureRecognizer
Kneading gesture Uipinchgesturerecognizer
Drag gesture Uipangesturerecognizer
Light sweep gesture Uiswipegesturerecognizer "4 separate directions"
Rotate gesture Uirotationgesturerecognizer
Long press gesture Uilongpressgesturerecognizer

Second, Uigesturerecognizer properties

Object properties:

Copy Code code as follows:

@property (nonatomic, ReadOnly) uigesturerecognizerstate state;/*< gesture Status * *
@property (nonatomic, getter=isenabled) BOOL enabled;/*< gestures are available * *
@property (nonatomic, readonly) UIView *view;/*< The view that triggers the gesture * *
@property (nonatomic, assign) BOOL delaystouchesbegan;/*< gesture recognition failed to perform a touch start event, the default is no * *

Gesture Recognition State:

Copy Code code as follows:

typedef ns_enum (Nsinteger, uigesturerecognizerstate) {
Uigesturerecognizerstatepossible,//has not yet recognized what gesture operation (but may have triggered a touch event), default state
Uigesturerecognizerstatebegan,//gesture has started, has been recognized at this time, but this process may be changed, gesture operation has not been completed
Uigesturerecognizerstatechanged,//gesture status change
uigesturerecognizerstateended,//Gesture recognition operation completed (at this point the finger has been released)
Uigesturerecognizerstatecancelled,//gesture canceled, revert to default state
Uigesturerecognizerstatefailed,//gesture recognition failed, revert to default state
Uigesturerecognizerstaterecognized = uigesturerecognizerstateended/gesture recognition complete, with uigesturerecognizerstateended
};

The left picture is the state transition of the short gesture, and the right image is the state transition of the long gesture.

Three, Uigesturerecognizer method

Copy Code code as follows:

#pragmX mark adds touch execution events
-(void) Addtarget: (ID) Target action: (SEL) Action;
#pragma mark removes touch execution events
-(void) Removetarget: (ID) Target action: (SEL) Action;
#pragma Mark's number of touch points (number of fingers at the same time)
-(Nsuinteger) numberoftouches;
#pragma Mark's relative position in the specified view
-(Cgpoint) Locationinview: (uiview*) view;
#pragma mark Touch Point relative to the location of the specified view
-(Cgpoint) Locationoftouch: (Nsuinteger) Touchindex InView: (uiview*) view;
#pragma mark specifies a gesture that requires another gesture to fail before it is executed.
-(void) Requiregesturerecognizertofail: (Uigesturerecognizer *) Othergesturerecognizer;

Iv. Use of Uigesturerecognizer

Copy Code code as follows:

# Create gesture Recognizer Objects
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
# Set the specific properties of the gesture Recognizer object
tap.numberoftapsrequired = 2; 2 beats in a row
tap.numberoftouchesrequired = 2;//needs 2 fingers to knock together
# Add gesture recognizer to the corresponding view
[Self.myview Addgesturerecognizer:tap];
# The trigger of the listening gesture
[Tap addtarget:self Action: @selector (Tapview:)];

V. Multi-gesture conflict

In iOS, if the recognition part of a gesture A is a subset of another gesture B, by default A is recognized and B is unrecognized.

For example, drag gesture A and light sweep gesture B, the light sweep gesture only at the end of the gesture to perform, and drag gestures at the beginning of the gesture to execute, the two gestures together to trigger, the light sweep gesture is a victim.

The solution to the gesture conflict is to use a method:

Copy Code code as follows:

-(void) Requiregesturerecognizertofail: (Uigesturerecognizer *) Othergesturerecognizer

This method specifies that a gesture requires another gesture recognition failure before it is executed.

For example, to resolve drag gestures and light sweep gestures conflict:

Copy Code code as follows:

The execution of the drag gesture requires the right light sweep gesture's failure as a prerequisite
[Pangesture Requiregesturerecognizertofail:swipegesturetoright];
The execution of a drag gesture requires the failure of the left light sweep gesture to be the prerequisite
[Pangesture Requiregesturerecognizertofail:swipegesturetoleft];

Six, uigesturerecognizerdelegate Agent

Proxy method:

Copy Code code as follows:

#pragma mark if a control recognizes gestures and continues to propagate gesture recognition along the responder chain, return no by default
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer;

Use this proxy method to implement the gesture of two different controls at the same time, you need to have the method return yes, and gesture recognition can be passed to different controls

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.