iOS gesture recognizer

Source: Internet
Author: User


Uigesturerecognizer


The Uigesturerecognizer class is used to detect and identify gestures used by users when they use the device. It is an abstract class that defines the basic behavior of all gestures. The following is a Uigesturerecognizer subclass that handles specific user gesture behavior:


UITapGestureRecognizer//1. Click
Uilongpressgesturerecognizer//3. Long Press
Uiswipegesturerecognizer//4. Swipe
Uipangesturerecognizer//5. Mobile
Uirotationgesturerecognizer//6. Rotate
Uipinchgesturerecognizer//7. Kneading

To create a gesture:

// 1. Click
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imgView addGestureRecognizer:tap];
    
    // 2. Double click
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [imgView addGestureRecognizer:doubleTap];
    
    // Double click to fail to click
    [tap requireGestureRecognizerToFail:doubleTap];
    
    // 3. Long press
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    / / set the minimum time
    longPress.minimumPressDuration = 1;
    [imgView addGestureRecognizer:longPress];
    
    
    // 4. Swipe
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // set the swipe direction
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [imgView addGestureRecognizer:swipe];
    
    // 5. Move
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imgView addGestureRecognizer:pan];
    
    // swipe to move before moving
    [pan requireGestureRecognizerToFail:swipe];
    
    // 6. Rotate
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imgView addGestureRecognizer:rotation];
    
    // 7. Kneading
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imgView addGestureRecognizer:pinch];


Gesture Trigger Event:


Gestureaction:


-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    If (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog (@" long press start");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog (@" long press end");
    }
}

- (void)panAction:(UIPanGestureRecognizer *)pan {

    //The coordinates of the finger
    CGPoint point = [pan locationInView:self.view];
    _view.center = point;
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{

    If (rotation.state == UIGestureRecognizerStateChanged) {
        
        / / Get the arc
        CGFloat angle = rotation.rotation;
        
        //Rotating
        Rotation.view.transform = CGAffineTransformMakeRotation(angle);
        
    } else if (rotation.state == UIGestureRecognizerStateEnded) {
        
        //reduction
        [UIView animateWithDuration:.5 animations:^{
            
            Rotation.view.transform = CGAffineTransformIdentity;
        }];
    }
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{

    If (pinch.state == UIGestureRecognizerStateChanged) {
        
        // Get the zoom ratio
        CGFloat scale = pinch.scale;
        
        // zoom
        Pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
        
    } else if (pinch.state == UIGestureRecognizerStateEnded) {
        
        [UIView animateWithDuration:.5 animations:^{
            
            Pinch.view.transform = CGAffineTransformIdentity;
        }];
    }
}
Motion Shake gesture
/ / Let the current object become the first responder
- (BOOL)canBecomeFirstResponder
{
          
    Return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    NSLog (@"shakes a start");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    NSLog (@"shakes the end");
} 


Recommend a detailed use of iOS gesture Recognition article: iOS gesture recognition



iOS 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.