An example of hand gesture recognition for IOS development _ios

Source: Internet
Author: User

It feels necessary to make a small summary of gesture recognition in iOS development. Here's how to use storyboard to add gestures to the corresponding controls, and then add gestures to our controls in pure code, and the gestures are simpler to use. And button is similar to the use, but also the target action callback, say not much, cut into today's topic.

There are six kinds of gesture recognition: light-struck gesture (tapgesturerecognizer), light sweep gesture (Swipegesturerecognizer), long press gesture (Longpressgesturerecognizer), Drag gesture (Pangesturerecognizer), kneading gesture (pinchgesturerecognizer), rotating gesture (rotationgesturerecognizer);

In fact, these gestures with the Touche event can be fully realized, Apple is the common touch event encapsulated into gestures, to provide to the user. Readers can use touchesmoved to write drag gestures, etc.

First, use storyboard to add gesture recognition to the control

1. With storyboard to add gesture recognition, and add a button to the same step, first we have to find the corresponding gestures, hand gesture recognition control to the control we want to add gestures, screenshot as follows:
2. Add the 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, and the screenshot is as follows:

Second, pure code to add gesture recognition

With storyboard can greatly simplify our operation, but the way of pure code or will, just like to dreamwear edit the Web page (of course, storyboard drag-and-drop function than dreamwear drag more powerful), with pure code to knock out more flexible, More convenient to maintain. But with storyboard can reduce our workload, these two need to cooperate with the use of can greatly improve our development efficiency. The personal feeling uses storyboard to put the frame up (the relationship between the controller), the small thing or uses the pure code to knock out is better. Here's how to add gesture recognition to our controls in a pure code way.

1. Add the light-click Gesture (Tapgesturerecognizer)

The code to initialize the code Tapgesturerecongnizer is as follows:

New tap gesture
 uitapgesturerecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action:@ Selector (tapgesture:)];
 Set the number of clicks and click Hand Index
 tapgesture.numberoftapsrequired = 1;//click times
 tapgesture.numberoftouchesrequired = 1;//click Hand Index
 [Self.view addgesturerecognizer:tapgesture];

Add the appropriate business logic to the callback method:

Light-click Gesture Trigger Method
-(void) Tapgesture: (ID) sender
{
 //The thing to do after a light hit  
}

2. Long press gesture (Longpressgesturerecognizer)
Initialization code:

Add long gesture
 uilongpressgesturerecognizer *longpressgesture = [[Uilongpressgesturerecognizer alloc] Initwithtarget: Self action: @selector (longpressgesture:)];
 Set long by time
 longpressgesture.minimumpressduration = 0.5;//(2 seconds)
 [Self.view Addgesturerecognizer: Longpressgesture];

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

Constant-Press gesture trigger method
-(void) Longpressgesture: (ID) sender
{
 Uilongpressgesturerecognizer *longpress = sender;
 if (longpress.state = = Uigesturerecognizerstatebegan)
 {
  Uialertview *alter = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "long press trigger" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:nil];
  [Alter show];
 }

Code Description: The common status of gestures is as follows

    • Start: Uigesturerecognizerstatebegan
    • Change: uigesturerecognizerstatechanged
    • End: uigesturerecognizerstateended
    • Cancellation: uigesturerecognizerstatecancelled
    • Failure: uigesturerecognizerstatefailed

3. Light sweep gesture (Swipegesturerecognizer)

When initializing the light sweep gesture, you have to specify the direction of the light sweep, up and down. If you want to add more than one light sweep direction, you have to add more than one light gesture, but the callback is the same method.

Add a light sweep gesture, one to the left, one to the right, and the following code:

Add a light sweep gesture
 Uiswipegesturerecognizer *swipegesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action:@ Selector (swipegesture:)];
 Set light sweep direction
 swipegesture.direction = uiswipegesturerecognizerdirectionright;//default to right
 [Self.view Addgesturerecognizer:swipegesture];
  
 Add a light sweep gesture
 Uiswipegesturerecognizer *swipegestureleft = [[Uiswipegesturerecognizer alloc] Initwithtarget:self Action: @selector (swipegesture:)];
 Set light sweep direction
 swipegestureleft.direction = uiswipegesturerecognizerdirectionleft;//default to right
 [Self.view Addgesturerecognizer:swipegestureleft];

The callback method is as follows:

Light sweep gesture Trigger method
-(void) Swipegesture: (ID) sender
{
 Uiswipegesturerecognizer *swipe = sender;
 if (swipe.direction = = Uiswipegesturerecognizerdirectionleft)
 {
  //left light sweep do things
 }
 if ( Swipe.direction = = uiswipegesturerecognizerdirectionright)
 {
  //Right light sweep do things
 }
}

4. Kneading gesture (Pinchgesturerecognizer)

Kneading gesture Initialization

Add kneading gesture
uipinchgesturerecognizer *pinchgesture = [[Uipinchgesturerecognizer alloc] initwithtarget:self action:@ Selector (pinchgesture:)];
[Self.view Addgesturerecognizer:pinchgesture];

Method of kneading gestures to trigger (enlarge or shrink the picture):

Kneading gesture Trigger method
-(void) Pinchgesture: (ID) sender
{
  Uipinchgesturerecognizer *gesture = sender;
  
 When the gesture changes
 if (gesture.state = = uigesturerecognizerstatechanged)
 {
  //kneading gestures in the Scale property record zoom ratio
  _ Imageview.transform = Cgaffinetransformmakescale (Gesture.scale, Gesture.scale);
 }
  
 After end recovery
 if (gesture.state==uigesturerecognizerstateended)
 {
  [UIView animatewithduration:0.5 animations:^{
   _imageview.transform = cgaffinetransformidentity;//cancel all deformations
  }}

5. Drag gesture (Pangesturerecognizer)

The initialization of the drag gesture

Add drag gesture
uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc] initwithtarget:self action:@ Selector (pangesture:)];
[Self.view Addgesturerecognizer:pangesture];

Drag the method you want to do (get the moved point by Translationinview, similar to the Touchesmoved method)

Drag gesture
-(void) Pangesture: (ID) sender
{
 Uipangesturerecognizer *pangesture = sender;
  
 Cgpoint movepoint = [Pangesture TranslationInView:self.view];
  
 Do what you want to do.

6. Rotating gesture (Rotationgesturerecognizer)

Initialization of a rotational gesture

Add rotate gesture
uirotationgesturerecognizer *rotationgesture = [[Uirotationgesturerecognizer alloc] Initwithtarget: Self action: @selector (rotationgesture:)];
[Self.view Addgesturerecognizer:rotationgesture];

Rotate gesture Call Method:

Rotate gesture
-(void) Rotationgesture: (ID) sender
{
  
 Uirotationgesturerecognizer *gesture = sender;
  
 if (gesture.state==uigesturerecognizerstatechanged)
 {
  _imageview.transform= Cgaffinetransformmakerotation (gesture.rotation);
 }
  
 if (gesture.state==uigesturerecognizerstateended)
 {
   
  [UIView animatewithduration:1 animations:^{
   _ imageview.transform=cgaffinetransformidentity;//cancel deformation
  }];
 }
  

The above things do not have much advanced technology, that is, the development of iOS in the gesture made a little summary, warm the basic knowledge. In the previous blog also useful to the content of gesture recognition, that is, there is no system to comb the knowledge of gesture recognition, this article to do a basic supplement it.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.