iOS common gesture recognizer

Source: Internet
Author: User

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

};

The delegate method is required to support multiple gestures at the same time, and returns YES to determine the simultaneous use of several gestures

The bool value returned by the method determines whether the view can respond to multiple gestures at the same time

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer

{

NSLog (@ "%@-%@", Gesturerecognizer.class, Othergesturerecognizer.class);

return YES;

}

1. Click on the recognizer

Create a click Gesture Recognizer and listen

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Taptouch)];

Set gesture Recognizer Properties

Click Response several times

tap.numberoftapsrequired = 2;

Several fingers simultaneously click

tap.numberoftouchesrequired = 2;

Add gesture recognizer

[Self.uiimageview Addgesturerecognizer:tap];

2. Long press the recognizer

Create a long press recognizer and listen

Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (Longpresstouch)];

Setting properties

How long response time

Longpress.minimumpressduration = 5;

Allow the finger to move offset before pressing the Post event response

Longpress.allowablemovement = 10;

Adding recognizers

[Self.view1 addgesturerecognizer:longpress];

3. Swipe

Supported gesture Direction enumeration types

typedef ns_options (Nsuinteger, uiswipegesturerecognizerdirection) {

Uiswipegesturerecognizerdirectionright = 1 << 0,//Right

Uiswipegesturerecognizerdirectionleft = 1 << 1,//left

Uiswipegesturerecognizerdirectionup = 1 << 2,//Up

Uiswipegesturerecognizerdirectiondown = 1 << 3//Down

};

Create a swipe recognizer and listen to create multiple swipe recognizers if you want to use multiple orientations at the same time

Example: Adding a left-to-right gesture

Left

Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (Swipehtouch )];

Swipe.direction = Uiswipegesturerecognizerdirectionleft;

[Self.view1 Addgesturerecognizer:swipe];

Right

Uiswipegesturerecognizer *swipe2 = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipehtouch)];

Swipe2.direction = Uiswipegesturerecognizerdirectionright;

[Self.view1 Addgesturerecognizer:swipe2];

/**

* Rotation, pinch and drag note: When the finger is released, it resets the value, and if you need to perform operations such as scaling and rotation, assign values to the returned value in the appropriate place

*/

4. Rotate

Create a rotation recognizer and listen

Uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector ( Rotationtouch:)];

[Self.view1 addgesturerecognizer:rotation];

Watch out.

-(void) Rotationtouch: (Uirotationgesturerecognizer *) routation

{

NSLog (@ "rotation");

Each time you add a previous angle

Self.view1.transform = Cgaffinetransformrotate (Self.view1.transform, routation.rotation);

Turn the rotation angle clear 0

routation.rotation = 0;

}

5. Kneading

Create a pinch gesture

Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (Pinchtouch :)];

[Self.view1 Addgesturerecognizer:pinch];

Watch out.

-(void) Pinchtouch: (Uipinchgesturerecognizer *) pinch

{

NSLog (@ "pinch");

Self.view1.transform = Cgaffinetransformscale (Self.view1.transform, Pinch.scale, Pinch.scale);

Pinch.scale = 1.0;

}

6. Dragging

Create a drag gesture

Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (Pantouch)];

[Self.view1 Addgesturerecognizer:pan];

Watch out.

-(void) Pantouch: (Uipangesturerecognizer *) pan

{

Get the coordinates of the move

Cgpoint point = [Pan TranslationInView:pan.view];

NSLog (@ "Drag and Drop");

Cgpoint temp = Self.view1.center;

temp.x + = Point.x;

Temp.y + = Point.y;

Self.view1.center = temp;

Must be added

[Pan Settranslation:cgpointzero InView:pan.view];

}

DEMO Link: http://pan.baidu.com/s/1qW3VmUg Password: 9pqk

iOS common gesture recognizer

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.