博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS-手势识别
阅读量:6655 次
发布时间:2019-06-25

本文共 6826 字,大约阅读时间需要 22 分钟。

/** 1. 使用手势识别的四个步骤  1> 实例化手势识别    - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> 将手势识别附加到指定的视图    addGestureRecognizer  4> 编写监听方法  用代码创建一个UIImageView添加到视图,实验6种手势识别。 3. 手势说明  0> UIGestureRecognizer     所有手势识别的父类,不允许直接使用,可以用来自定义手势(建议大家不要挑战)     最常用的属性:     view:   发生手势的视图    state:  手势当前的状态,主要用于连续手势,对于离散手势一般不使用    1> UITapGestureRecognizer          点按手势(离散手势,其他手势都是连续手势)     属性:    numberOfTapsRequired       点击次数,单击双击    numberOfTouchesRequired    手指根数  2> UILongPressGestureRecognizer    长按手势     属性不建议修改  3> UIPanGestureRecognizer          拖动手势     属性:不常用    方法:    // 在视图中拖动的距离    - (CGPoint)translationInView:(UIView *)view;    // 在视图中拖动的速度,通常可用于模拟惯性,需要一些物理方面的计算     - (CGPoint)velocityInView:(UIView *)view;  4> UIPinchGestureRecognizer        捏合手势     属性:    scale           比例    velocity        捏合速度,不常用  5> UIRotationGestureRecognizer     旋转手势     属性    rotation        旋转角度    velocity        旋转速度,不常用  6> UISwipeGestureRecognizer        轻扫手势,通常添加到根视图上        属性    numberOfTouchesRequired     参与轻扫手势的手指根数    direction                   轻扫的方向     提示:    1) 如果要检测几个方向的轻扫,需要分别实例化几个轻扫手势    2) 轻扫手势虽然是连续手势,但是不需要去处理UIGestureRecognizerStateChanged状态        因为是在手指离开屏幕后,该手势才被识别的  其他技巧     在if语句中,将常量放置在判断条件的左侧,可以避免因为只写了 = 出现警告,置之不理的情况! */- (void)viewDidLoad{    [super viewDidLoad];        // 0. 实例化UIImageView    UIImage *image = [UIImage imageNamed:@"001"];    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];        [imageView setFrame:CGRectMake(10, 100, 300, 196)];    [self.view addSubview:imageView];        [imageView setUserInteractionEnabled:YES];        // 1. 添加点按手势    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];    // 定义双击    tap.numberOfTapsRequired = 2;    [imageView addGestureRecognizer:tap];        // 2. 添加长按手势    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];    [imageView addGestureRecognizer:longTap];        // 3. 拖动手势    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];        [imageView addGestureRecognizer:pan];        // 4. 捏合手势    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];    [imageView addGestureRecognizer:pinch];        // 5. 旋转手势    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];    [imageView addGestureRecognizer:rotation];        // 6. 轻扫手势    // 如果要做清扫手势,需要分别实例化4个方向的手势    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];        swipeUp.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;        [self.view addGestureRecognizer:swipeUp];    //    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];//    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;//    [self.view addGestureRecognizer:swipeDown];//    //    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];//    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;//    [self.view addGestureRecognizer:swipeLeft];////    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];//    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;//    [self.view addGestureRecognizer:swipeRight];}#pragma mark - 点按手势- (void)tap:(UITapGestureRecognizer *)recognizer{    // 双击放大图片    UIImageView *imageView = (UIImageView *)recognizer.view;    [UIView animateWithDuration:0.8f animations:^{        [imageView setTransform:CGAffineTransformScale(imageView.transform, 2.0, 2.0)];    } completion:^(BOOL finished) {        [UIView animateWithDuration:0.5f animations:^{            [imageView setTransform:CGAffineTransformScale(imageView.transform, 0.5, 0.5)];        }];    }];}#pragma mark - 长按手势- (void)longTap:(UILongPressGestureRecognizer *)recognizer{    NSLog(@"长按");    // 长按旋转图片    // 开始长按    // 在形变时,iOS采取就近原则旋转,如果不能按照希望的方向旋转,可以增加一些修正值,例如0.01    // 直接通过形变属性,要实现一次性转一圈,比较困难,可以分两次进行    if (UIGestureRecognizerStateBegan == recognizer.state) {        [UIView animateWithDuration:0.8f animations:^{            [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)];        }];    } else if (UIGestureRecognizerStateEnded == recognizer.state) {        [UIView animateWithDuration:0.8f animations:^{            [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, M_PI - 0.01)];        } completion:^(BOOL finished) {            // 清除所有的形变属性            [recognizer.view setTransform:CGAffineTransformIdentity];        }];    }}#pragma mark - 拖动- (void)pan:(UIPanGestureRecognizer *)recognizer{    CGPoint translation = [recognizer translationInView:self.view];        // 让图像根据手指移动    if (UIGestureRecognizerStateChanged == recognizer.state) {        // 形变参数,有Make的时候,是相对初始位置计算的        // 没有Make是递增修改形变的        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];                // 拖动手指中的平移距离是相对于初始位置,如果使用CGAffineTransformTranslate累加形变方法        // 需要在每次位移之后,重置recognizer的位移量,就是将位移量清零        [recognizer setTranslation:CGPointZero inView:recognizer.view];    }}#pragma mark - 捏合- (void)pinch:(UIPinchGestureRecognizer *)recognizer{    // 调整图像的显示比例    if (UIGestureRecognizerStateChanged == recognizer.state) {                // 等比例缩放        [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale)];                // 在设置累加形变参数时,需要充值手势的缩放比例        recognizer.scale = 1.0f;    }}#pragma mark - 旋转- (void)rotate:(UIRotationGestureRecognizer *)recognizer{    if (UIGestureRecognizerStateChanged == recognizer.state) {        recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);                // 重置旋转量        recognizer.rotation = 0.0f;    }}#pragma mark - 轻扫- (void)swipe:(UISwipeGestureRecognizer *)recognizer{    //NSLog(@"%d", recognizer.direction);    if (UISwipeGestureRecognizerDirectionDown == recognizer.direction) {        NSLog(@"向下");    } else if (UISwipeGestureRecognizerDirectionUp == recognizer.direction) {        NSLog(@"向上");    } else if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {        NSLog(@"向左");    } else if (UISwipeGestureRecognizerDirectionRight == recognizer.direction) {        NSLog(@"向右");    }}

 

转载于:https://www.cnblogs.com/DarbyCJ/p/3671214.html

你可能感兴趣的文章
pythonif条件语句实例_Python基础之条件控制操作实例【if语句】
查看>>
excel操作题_Excel|操作题第4套
查看>>
i5-1135g7相当于什么水平_剑桥英语I优加-剑桥pet相当于国内什么水平
查看>>
mysql8.0150安装教程_MySQL8安装Installer版的图文教程
查看>>
mysql 缓冲区_mysql 系列 缓冲区
查看>>
mysql tablespaces_MySQL8.0-MySQLTablespace
查看>>
mysql enum创建_mysql – 如何在SQLite中创建ENUM类型?
查看>>
mysql5.5.7无法创建.pid_MySQL启动创建不了pid怎么办
查看>>
马哥数据库mysql笔记_马哥学习笔记五MYSQL初步
查看>>
通信原理知识点总结_通信原理课程总结(一)
查看>>
mysql中想选出最新的日期_mysql – 从特定日期开始选择最新版本
查看>>
mysql 二进制日志恢复_使用mysql二进制日志恢复数据
查看>>
mysql 统计信息收集_Oracle 收集统计值 收集统计信息
查看>>
MySQL安装显示路径冲突_linux下的mysql安装路径冲突怎么办
查看>>
django mysql增删查_使用django连接数据库 对数据库 增删改查
查看>>
mysql初次安装_Mysql初始安装
查看>>
mysql源码包最新版本_1mysql源码安装到时候要更新
查看>>
python win32ui_Python win32ui.CreateWindowFromHandle方法代码示例
查看>>
mysql 序列重置不好用_二十九、MySQL 序列使用
查看>>
python字频统计软件_python统计文本中字频的方法
查看>>