Gesture Recognition for iOS development

Source: Internet
Author: User

One, add gesture recognition to the control with storyboard

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

1. Add a tap gesture (Tapgesturerecognizer)

The code for the initialization code Tapgesturerecongnizer is as follows:

123456 //新建tap手势    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];    //设置点击次数和点击手指数    tapGesture.numberOfTapsRequired = 1; //点击次数    tapGesture.numberOfTouchesRequired = 1; //点击手指数    [self.view addGestureRecognizer:tapGesture];

Add the appropriate business logic in the callback method:

12345 //轻击手势触发方法-(void)tapGesture:(id)sender{    //轻击后要做的事情        }

2. Long press gesture (Longpressgesturerecognizer)
Initialization code:

12345     //添加长摁手势    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];    //设置长按时间    longPressGesture.minimumPressDuration = 0.5; //(2秒)    [self.view addGestureRecognizer:longPressGesture];

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

12345678910 //常摁手势触发方法-(void)longPressGesture:(id)sender{    UILongPressGestureRecognizer *longPress = sender;    if(longPress.state == UIGestureRecognizerStateBegan)    {        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发"delegate:nil cancelButtonTitle:@"取消"otherButtonTitles: nil];        [alter show];    }}

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:

1234567891011 //添加轻扫手势    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];    //设置轻扫的方向    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右    [self.view addGestureRecognizer:swipeGesture];        //添加轻扫手势    UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];    //设置轻扫的方向    swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右    [self.view addGestureRecognizer:swipeGestureLeft];

The callback method is as follows:

12345678910111213 //轻扫手势触发方法-(void)swipeGesture:(id)sender{    UISwipeGestureRecognizer *swipe = sender;    if(swipe.direction == UISwipeGestureRecognizerDirectionLeft)    {        //向左轻扫做的事情    }    if(swipe.direction == UISwipeGestureRecognizerDirectionRight)    {        //向右轻扫做的事情    }}

4. Pinch gesture (Pinchgesturerecognizer)

Pinch gesture Initialization

123 //添加捏合手势UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture];

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

1234567891011121314151617181920 ////捏合手势触发方法-(void) pinchGesture:(id)sender{     UIPinchGestureRecognizer *gesture = sender;        //手势改变时    if(gesture.state == UIGestureRecognizerStateChanged)    {        //捏合手势中scale属性记录的缩放比例        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);    }        //结束后恢复    if(gesture.state==UIGestureRecognizerStateEnded)    {        [UIView animateWithDuration:0.5 animations:^{            _imageView.transform = CGAffineTransformIdentity;//取消一切形变        }];    }}

5. Drag gestures (Pangesturerecognizer)

Initialization of the drag gesture

123 //添加拖动手势UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];

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

123456789 //拖动手势-(void) panGesture:(id)sender{    UIPanGestureRecognizer *panGesture = sender;        CGPoint movePoint = [panGesture translationInView:self.view];        //做你想做的事儿}

6. Rotation gesture (Rotationgesturerecognizer)

The initialization of the rotation gesture

123 //添加旋转手势UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];

How to invoke a rotation gesture:

1234567891011121314151617181920 //旋转手势-(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;//取消形变        }];    }    }

Gesture Recognition for iOS development

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.