Gesture Recognition for iOS development

Source: Internet
Author: User

Feel the need to make a small summary of gesture recognition in iOS development. In the previous iOS development custom emoji keyboard (component encapsulation and auto layout) blog used a flick gesture, that is, when tapping textview from the emoji keyboard back to the system keyboard, in the TextView hand is added with storyboard. We'll show you how to use storyboard to add gestures to the corresponding controls, and then add gestures to our controls in a purely code way, using gestures that are easier to use. Similar to the use of button, but also the target action callback, words not to say, cut into today's topic.  There are six gesture recognition: Tap gesture (Tapgesturerecognizer), swipe gesture (Swipegesturerecognizer), long-press gesture (Longpressgesturerecognizer), Drag gestures (pangesturerecognizer), pinch gestures (pinchgesturerecognizer), rotation gestures (rotationgesturerecognizer);

In fact, these gestures are fully achievable with the Touche event, and Apple is wrapping the usual touch events into gestures to provide the user. Readers can use touchesmoved to write drag gestures, etc.

One, using storyboard to add gesture recognition to the control, of course, with storyboard to cut a picture

1. To add gesture recognition with storyboard, just like the step of adding a button, first we have to find the appropriate gesture to drag the gesture recognition control into the control we want to add the gesture to, as follows:


2. Add a callback event to the gesture we dragged out, and make no difference to the button callback event, add the business logic to be implemented in the callback method, as follows:

  

Two, pure code to add gesture recognition

Using storyboard can greatly simplify our operation, but the pure code way still will, just like to dreamwear edit the Web page (of course, storyboard drag and drop more powerful than dreamwear drag), with pure code to knock out more flexible, Easier to maintain. However, the use of storyboard can reduce our workload, the two should be used in conjunction with the use to greatly improve our development efficiency. Personal feeling use storyboard to set up the frame (the relationship between the controller), the small things or use pure code to knock out better. Here's how to add gesture recognition to our controls in a pure code way.

1. Add a tap gesture (Tapgesturerecognizer)

The code for the initialization code Tapgesturerecongnizer is as follows:

1     // New tap gesture 2     UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tapgesture:)]; 3     // set number of clicks and click -to-hand index 4     1 // Click Count 5     1 // Click Hand Index 6     [Self.view Addgesturerecognizer:tapgesture];

Add the appropriate business logic in the callback method:

1 // tap Gesture Trigger method 2 -(void) Tapgesture: (ID) sender3{4     // what to do after a        light click 5 }

2. Long press gesture (Longpressgesturerecognizer)

Initialization code:

1     // add a long push gesture 2     Uilongpressgesturerecognizer *longpressgesture = [[Uilongpressgesturerecognizer alloc] Initwithtarget:self Action: @selector (longpressgesture:)]; 3     // set long by Time 4     0.5 // (2 seconds) 5     [Self.view Addgesturerecognizer:longpressgesture];

Add the appropriate method to the corresponding callback method (executed when the gesture starts):

1 //frequent gesture triggering method2-(void) Longpressgesture: (ID) Sender3 {4Uilongpressgesturerecognizer *longpress =Sender;5     if(Longpress.state = =Uigesturerecognizerstatebegan)6     {7Uialertview *alter = [[Uialertview alloc] Initwithtitle:@"Tips"Message@"Long Press Trigger" Delegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil];8 [Alter show];9     }Ten}

Code Description: The common status of gestures is as follows

Start: Uigesturerecognizerstatebegan

Change: uigesturerecognizerstatechanged

Ends: uigesturerecognizerstateended

Cancel: uigesturerecognizerstatecancelled

Failure: uigesturerecognizerstatefailed

3. Swipe gesture (Swipegesturerecognizer)

When you initialize a swipe gesture, you have to specify the direction of the swipe, up or down. If you want to add multiple swipe directions, you have to add multiple swipe gestures, but the callback is the same method.

To add a swipe gesture, one left to the right, the code is as follows:

1     //add a swipe gesture2Uiswipegesturerecognizer *swipegesture =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)];3     //set the direction of the swipe4Swipegesture.direction = Uiswipegesturerecognizerdirectionright;//Default Right5 [Self.view addgesturerecognizer:swipegesture];6     7     //add a swipe gesture8Uiswipegesturerecognizer *swipegestureleft =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)];9     //set the direction of the swipeTenSwipegestureleft.direction = Uiswipegesturerecognizerdirectionleft;//Default Right One[Self.view Addgesturerecognizer:swipegestureleft];

The callback method is as follows:

1 //Swipe gesture Trigger method2-(void) Swipegesture: (ID) Sender3 {4Uiswipegesturerecognizer *swipe =Sender;5     if(Swipe.direction = =uiswipegesturerecognizerdirectionleft)6     {7         //Swipe left to do things8     }9     if(Swipe.direction = =uiswipegesturerecognizerdirectionright)Ten     { One         //Swipe right to do things A     } - } -     

4. Pinch gesture (Pinchgesturerecognizer)

Pinch gesture Initialization

1     // Add a pinch gesture 2     Uipinchgesturerecognizer *pinchgesture = [[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector ( Pinchgesture:)]; 3     [Self.view Addgesturerecognizer:pinchgesture];

How to trigger a pinch gesture (enlarge or shrink the image):

1 ////Pinch gesture triggering method2-(void) Pinchgesture: (ID) Sender3 {4Uipinchgesturerecognizer *gesture =Sender;5     6     //when gestures change7     if(Gesture.state = =uigesturerecognizerstatechanged)8     {9         //scaling of the scale property record in a pinch gestureTen_imageview.transform =Cgaffinetransformmakescale (Gesture.scale, gesture.scale); One     } A      -     //Recover after end -     if(gesture.state==uigesturerecognizerstateended) the     { -[UIView animatewithduration:0.5animations:^{ -_imageview.transform = cgaffinetransformidentity;//Cancel all deformation -         }]; +     } -}

5. Drag gestures (Pangesturerecognizer)

Initialization of the drag gesture

1     // Add a drag gesture 2     Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Pangesture:)]; 3     [Self.view Addgesturerecognizer:pangesture];

The way to do the drag gesture (get moving points by Translationinview, similar to the Touchesmoved method)

1 // Drag gestures 2 -(void) Pangesture: (ID) sender3{4     Uipangesturerecognizer *pangesture = sender; 5     6     Cgpoint movepoint = [Pangesture TranslationInView:self.view]; 7     8     // do what you want to do. 9 }

  

6. Rotation gesture (Rotationgesturerecognizer)

The initialization of the rotation gesture

1     // Add a rotation gesture 2     Uirotationgesturerecognizer *rotationgesture = [[Uirotationgesturerecognizer alloc] initwithtarget:self action:@ Selector (rotationgesture:)]; 3     [Self.view Addgesturerecognizer:rotationgesture];

How to invoke a rotation gesture:

1 //Rotate gestures2-(void) Rotationgesture: (ID) Sender3 {4     5Uirotationgesturerecognizer *gesture =Sender;6     7     if(gesture.state==uigesturerecognizerstatechanged)8     {9_imageview.transform=cgaffinetransformmakerotation (gesture.rotation);Ten     } One      A     if(gesture.state==uigesturerecognizerstateended) -     { -          the[UIView animatewithduration:1animations:^{ -_imageview.transform=cgaffinetransformidentity;//Cancel the deformation -         }]; -     } +      -}

  

The above things do not have much advanced technology, is the iOS development of gestures made a little summary, the basic knowledge of temperature. In the previous blog also useful to the content of gesture recognition, is not a systematic comb the knowledge of gesture recognition, this blog to do a basic supplement it. Welcome to criticize correct, reprint please indicate source.

Gesture Recognition for iOS development

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.