美文网首页
02.3--iOS手势识别

02.3--iOS手势识别

作者: weyan | 来源:发表于2018-11-26 08:30 被阅读0次

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

  • 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

相关文章

  • 02.3--iOS手势识别

    一、手势识别(点按、长按、轻扫) 1、点击手势在控制器view上添加一个iamgeView 2、长按手势 3、轻扫...

  • 手势控制:点击、滑动、平移、捏合、旋转、长按、轻扫

    手势识别器(Gesture Recognizer)用于识别触摸序列并触发响应事件。当手势识别器识别到一个手势或手势...

  • 3.6 iOS手势识别的状态和手势识别器幕后原理

    2.2手势识别的状态和手势识别器幕后原理 (一)手势的状态 (二)离散型手势识别器和连续型手势识别器之间的对比: ...

  • Gesture手势

    手势识别器 手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们...

  • 手势识别

    手势识别 6种手势识别 在iOS开发中有6中手势识别:点按、捏合、拖动、轻扫、旋转、长按苹果推出手势识别主要是为了...

  • UIGestureRecognizer

    什么是手势识别器? 手势识别器就是对触摸事件做了封装,我们不需要判断某个手势是否触发,手势识别器本身起到了识别作用...

  • 手势——UIGestureRecognizer

    一、简介 UIGestureRecognizer是具体手势识别器的基类。 手势识别器对象(或简单地说是手势识别器)...

  • EasyAR手势姿势识别

    简介 简单试了下EasyAR的手势识别以及姿势识别 手势识别 目前官方是有两种手势 测试了感觉识别度蛮高的 也蛮快...

  • iOS手势识别

    UIGestureRecognizer手势识别器手势识别器是特殊的触摸事件UIGestureRecognizer是...

  • UIGestureRecognizer手势识别器学习笔记

    UIGestureRecognizer 具体手势识别器的基类。一个手势识别器对象,或者简单地说一个手势识别器,解耦...

网友评论

      本文标题:02.3--iOS手势识别

      本文链接:https://www.haomeiwen.com/subject/zjnptqtx.html