02.3--iOS手势识别

一、手势识别(点按、长按、轻扫)

  • 1、点击手势
    在控制器view上添加一个iamgeView
#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageV.userInteractionEnabled = YES;
    [self tapGes];
}
//1.创建点按手势
- (void)tapGes {
    //1.创建点按手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    //设置代理,点击右边可以触发事件,点击左边不可以
    tapGes.delegate = self;
    
    //2.添加手势
    [self.imageV addGestureRecognizer:tapGes];
    
}

//是否允许接收手指
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    //获取当前的点.
    CGPoint curP = [touch locationInView:self.imageV];
    //判断点在图片的左边还是右边;
    if (curP.x > self.imageV.bounds.size.width * 0.5) {
        //如果在右边,返回YES;
        return YES;
    }else {
        //否则返回NO;
        return NO;
    }
}

//实现点按手势方法
- (void)tap:(UITapGestureRecognizer *)tapGes {
    //点击后只有UIGestureRecognizerStateEnded状态
//    if (tapGes.state == UIGestureRecognizerStateBegan) {
//        NSLog(@"UIGestureRecognizerStateBegan");
//    }else if (tapGes.state == UIGestureRecognizerStateChanged) {
//        NSLog(@"UIGestureRecognizerStateChanged");
//    }else if (tapGes.state == UIGestureRecognizerStateEnded) {
//        NSLog(@"UIGestureRecognizerStateEnded");
//    }
    NSLog(@"%s",__func__);
}

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

@end
  • 2、长按手势
#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageV.userInteractionEnabled = YES;
    [self longPressGes];
}
//2.创建长按手势
- (void)longPressGes {
    
    //1.创建长按手势
    UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGes:)];
    
    //2.添加手势
    [self.imageV addGestureRecognizer:longP];
}


//长按手势分状态,长按移动时,会持续调用
- (void)longGes:(UILongPressGestureRecognizer *)longP {

    if (longP.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
    }else if (longP.state == UIGestureRecognizerStateChanged) {
        NSLog(@"UIGestureRecognizerStateChanged");
    }else if (longP.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");
    }
}

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

@end
  • 3、轻扫手势
#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageV.userInteractionEnabled = YES;
    [self swipeGesture];
}
//3、轻扫手势
-(void)swipeGesture{
    //添加手势
    //1.创建轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
    
    //默认是向右轻扫
    //设置轻扫的方向
    //一个轻扫手势只能对应一个方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
    //2.添加手势
    [self.imageV addGestureRecognizer:swipe];
    
    //1、再创建轻扫
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGes:)];
    
    //默认是向右轻扫
    //设置轻扫的方向
    //一个轻扫手势只能对应一个方向
    swipe2.direction = UISwipeGestureRecognizerDirectionRight;
    
    //2、添加手势
    [self.imageV addGestureRecognizer:swipe2];
}

//轻扫手势实现的方法
- (void)swipeGes:(UISwipeGestureRecognizer *)swipe {
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"UISwipeGestureRecognizerDirectionLeft");
    }else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
         NSLog(@"UISwipeGestureRecognizerDirectionRight");
    }
}

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

@end

二、手势识别(拖动(平移)、缩放、旋转)

1、平移

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageV.userInteractionEnabled = YES;
    
//    [self pinchGes];
//    [self rotationGes];
    [self panGes];
    
    //默认是不能够同时支持多个手势
    //可以通过代理进行设置同时实现多个手势.
}
//1.平移手势
- (void)panGes {
    //添加手势
    //1.创建平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    //2.添加手势
    [self.imageV addGestureRecognizer:pan];
}

//当拖动时持续调用
- (void)pan:(UIPanGestureRecognizer *)pan {
    //获取偏移量
    //获取的偏移量是相对于最原始的点
    CGPoint transP = [pan translationInView:self.imageV];
    NSLog(@"当前偏移量:%@",NSStringFromCGPoint(transP));
    
    //self.imageV.transform = CGAffineTransformMakeTranslation(transP.x, transP.y);
    self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);
    
    //清0操作(不让偏移量进行累加,获取的是相对于上一次的值,每一次走的值.)
    [pan setTranslation:CGPointMake(0, 0) inView:self.imageV];
}

@end

2、旋转手势

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageV.userInteractionEnabled = YES;
 
    [self rotationGes];
    
    //默认是不能够同时支持多个手势
    //可以通过代理进行设置同时实现多个手势.
}
//2.旋转手势
- (void)rotationGes {
    //1.创建旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    //2.添加手势
    [self.imageV addGestureRecognizer:rotation];
}

//当旋转手势时调用方法
- (void)rotation:(UIRotationGestureRecognizer *)rotationGes {
    //获取旋转角度(已经是弧度)
    //相对于最原始的弧度
    CGFloat rotatinon = rotationGes.rotation;
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotatinon);
    //清0
    [rotationGes setRotation:0];
}

@end

3、捏合手势(缩放手势)

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageV.userInteractionEnabled = YES;
 
    [self pinchGes];
    
    //默认是不能够同时支持多个手势
    //可以通过代理进行设置同时实现多个手势.
}
//3.创建捏合手势(缩放手势)
- (void)pinchGes {
    //添加手势
    //1.创建捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    
    //2.添加手势
    [self.imageV addGestureRecognizer:pinch];
}

//缩放手势调用的方法
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    //放大, 缩小
    //获取缩放比例(相对于最原始的比例)
    CGFloat scale = pinch.scale;
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, scale, scale);
    
    [pinch setScale:1];
}

@end

4、同时实现多个手势

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageV.userInteractionEnabled = YES;
    
    [self pinchGes];
    [self rotationGes];
    
    //一个手势默认是不能够同时支持多个手势
    //可以通过代理进行设置同时实现多个手势.
}

//是否允许同时支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

//3.创建捏合手势(缩放手势)
- (void)pinchGes {
    //添加手势
    //1.创建捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    
    //2.添加手势
    [self.imageV addGestureRecognizer:pinch];
}
//2.旋转手势
- (void)rotationGes {
    //1.创建旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    //2.添加手势
    [self.imageV addGestureRecognizer:rotation];
}

//当旋转手势时调用方法
- (void)rotation:(UIRotationGestureRecognizer *)rotationGes {
    //获取旋转角度(已经是弧度)
    //相对于最原始的弧度
    CGFloat rotatinon = rotationGes.rotation;
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotatinon);
    //清0
    [rotationGes setRotation:0];
}
@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实...
    翘楚iOS9阅读 3,014评论 0 13
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,019评论 3 119
  • 皓首苍颜嗜遐思 枯爪搅翰池 推敲切磋精炼字 冥思苦想力避平庸词 不知冬临岁飘逝 人生一霎时 浮生若梦留赋诗 不悔今...
    石姥姥阅读 177评论 12 19
  • wuwenjuan阅读 110评论 0 0
  • 我 的 相 机 秦北 我的相机 是我的一双眼睛 我看见 蜜蜂在花间飞舞 春燕在水边衔泥 蚕儿在盒里吐丝 我看...
    秦北阅读 206评论 0 3