手势识别 Gesture Recognizer

手势识别 Gesture Recognizer_第1张图片
图片来自500px

文 || 張贺

点按(UITapGestureRecognizer)

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    //设置代理
    tap.delegate = self;
    //2.添加手势
    [self.imageV addGestureRecognizer:tap];
}
//3.实现手势方法
- (void)tap{
    NSLog(@"%s",__func__);
}

#pragma mark - UIGestureRecognizerDelegate
//是否允许接收手指
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    //一张图片,左边不能点按,右边可以点按
    CGPoint currentPoint = [touch locationInView:self.imageV];
    if (currentPoint.x > self.imageV.bounds.size.width * 0.5) {
        //右边,可以点击
        return YES;
    }else{
        //左边,不可点击
        return NO;
    }
}
@end

长按(UILongPressGestureRecognizer)

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    //2.添加手势
    [self.imageV addGestureRecognizer:longPress];
}
//3.实现手势方法
//长按时调用(当长按手指拖动的时候该方法会持续调用)
- (void)longPress:(UILongPressGestureRecognizer *)longPress{

    /**
    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    
        UIGestureRecognizerStatePossible,
        UIGestureRecognizerStateBegan,
        UIGestureRecognizerStateChanged,
        UIGestureRecognizerStateEnded,
        UIGestureRecognizerStateCancelled,
        UIGestureRecognizerStateFailed,
        UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
    };
     */

    switch (longPress.state) {
        case UIGestureRecognizerStateBegan: NSLog(@"开始长按"); break;
        case UIGestureRecognizerStateChanged: NSLog(@"长按拖动"); break;
        case UIGestureRecognizerStateEnded: NSLog(@"结束长按"); break;
        default: break;
    }
}
@end

轻扫(UISwipeGestureRecognizer)

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    //设置清扫的方向,默认是Right
    /**
    typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
        UISwipeGestureRecognizerDirectionRight = 1 << 0,
        UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
        UISwipeGestureRecognizerDirectionUp    = 1 << 2,
        UISwipeGestureRecognizerDirectionDown  = 1 << 3
    };
     */
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    //2.添加手势
    [self.imageV addGestureRecognizer:swipe];

    //如果想实现向左向右都可以清扫,那么只能在创建一个手势
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

    [self.imageV addGestureRecognizer:swipe2];
}
//3.实现手势方法
- (void)swipe:(UISwipeGestureRecognizer *)swipe{
    //判断是向左轻扫还是向右轻扫
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"left");
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
        NSLog(@"Right");
    }
}
@end

拖动(UIPanGestureRecognizer)

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    //2.添加手势
    [self.imageV addGestureRecognizer:pan];

}
//3.实现手势方法
-(void)pan:(UIPanGestureRecognizer *)pan{
    //获取偏移量(相对于最原始的偏移量)
    CGPoint transP = [pan translationInView:self.imageV];
    //平移
    self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
    //让他相对于上一次(复位)
    [pan setTranslation:CGPointZero inView:self.imageV];
}
@end

捏合(UIPinchGestureRecognizer)

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    //2.添加手势
    [self.imageV addGestureRecognizer:pinch];

}
//3.实现手势方法
- (void)pinch:(UIPinchGestureRecognizer *)pinch{
    //缩放
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pinch.scale, pinch.scale);
    //复位
    [pinch setScale:1];
}
@end

旋转(UIRotationGestureRecognizer)

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
    //2.添加手势
    [self.imageV addGestureRecognizer:rotation];

}
//3.实现手势方法
-(void)rotationGes:(UIRotationGestureRecognizer *)rotationGes{
    //旋转
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotationGes.rotation);
    //复位
    [rotationGes setRotation:0];
}  
@end

注意:如果想要同时缩放和旋转,需要询问代理是否允许这样做
#pragma mark - UIGestureRecognizerDelegate
//Simultaneously : 同时地
//是否允许两个手势同时进行
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
//允许两个手势同时进行
return YES;
}

你可能感兴趣的:(手势识别 Gesture Recognizer)