2018-09-06 UIPanGestureRecognizer

//
// ViewController.m
// 22
//
// Created by xjk on 2018/9/6.
// Copyright © 2018年 xjk. All rights reserved.
//

import "ViewController.h"

CGFloat const gestureMinimumTranslation =20.0;
typedef enum:NSInteger{

kCameraMoveDirectionNone,

kCameraMoveDirectionUp,

kCameraMoveDirectionDown,

kCameraMoveDirectionRight,

kCameraMoveDirectionLeft

}CameraMoveDirection;
@interface ViewController ()
{
CameraMoveDirection direction;
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIPanGestureRecognizer*recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    [self.view addGestureRecognizer:recognizer];
    }

  • (void)handleSwipe:(UIPanGestureRecognizer*)gesture

{

CGPoint translation = [gesture translationInView:self.view];

if(gesture.state ==UIGestureRecognizerStateBegan)
    
{
    
    direction = kCameraMoveDirectionNone;
    
}

else if(gesture.state ==UIGestureRecognizerStateChanged&& direction == kCameraMoveDirectionNone)

{
    
    direction = [self determineCameraDirectionIfNeeded:translation];
    
    // ok, now initiate movement in the direction indicated by the user's gesture
    
    switch(direction) {
            
        case kCameraMoveDirectionDown:
            
            NSLog(@"Start moving down");
            
            break;
            
        case kCameraMoveDirectionUp:
            
            NSLog(@"Start moving up");
            
            break;
            
        case kCameraMoveDirectionRight:
            
            NSLog(@"Start moving right");
            
            break;
            
        case kCameraMoveDirectionLeft:
            
            NSLog(@"Start moving left");
            
            break;
            
        default:
            
            break;
            
    }
    
}

else if(gesture.state ==UIGestureRecognizerStateEnded)

{
    
    // now tell the camera to stop
    
    NSLog(@"Stop");
    
}

}

// This method will determine whether the direction of the user's swipe

  • (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation

{

if(direction != kCameraMoveDirectionNone)
    
    return direction;

// determine if horizontal swipe only if you meet some minimum velocity

if(fabs(translation.x) > gestureMinimumTranslation)
    
{
    
    BOOL gestureHorizontal = NO;
    
    if(translation.y ==0.0)
        
        gestureHorizontal = YES;
    
    else
        
        gestureHorizontal = (fabs(translation.x / translation.y) >5.0);
    
    if(gestureHorizontal)
        
    {
        
        if(translation.x >0.0)
            
            return kCameraMoveDirectionRight;
        
        else
            
            return kCameraMoveDirectionLeft;
        
    }
    
}

// determine if vertical swipe only if you meet some minimum velocity

else if(fabs(translation.y) > gestureMinimumTranslation)

{
    
    BOOL gestureVertical = NO;
    
    if(translation.x ==0.0)
        
        gestureVertical = YES;
    
    else
        
        gestureVertical = (fabs(translation.y / translation.x) >5.0);
    
    if(gestureVertical)
        
    {
        
        if(translation.y >0.0)
            
            return kCameraMoveDirectionDown;
        
        else
            
            return kCameraMoveDirectionUp;
        
    }
    
}

return direction;

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

你可能感兴趣的:(2018-09-06 UIPanGestureRecognizer)