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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,874评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,102评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,676评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,911评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,937评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,935评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,860评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,660评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,113评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,363评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,506评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,238评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,861评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,486评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,674评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,513评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,426评论 2 352

推荐阅读更多精彩内容