• UI- 五种手势识别总结


    #pragma mark - 手势  总共有五种手势  分别为 Tap点击 Pan拖拽 LongPress长时间按压 Pinch捏合手势 rotation旋转

     1. 定义成员变量

        UIImageView *_imgView; 定义UIImageView, 响应手势方法时调用

        CGPoint originalCenter;  记录一下拖拽手势起始位置

        CGAffineTransform  originalTrans; 记录一下捏合、旋转手势起始位置

     2. 定义ImageView

        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];

        imgView.image = [UIImage imageNamed:@"btn_02"];

        [self.view addSubview:imgView];

        _imgView = imgView; //赋值

     3. 打开与用户的交互能力 (至关重要,不能忘记)

        imgView.userInteractionEnabled = YES;

     4.Tap手势 --> 点击1次/两次

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onceTapped )];

        把这个手势添加到视图中

        [imgView addGestureRecognizer:tap];

        UITapGestureRecognizer *dblTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twiceTapped)];

        dblTap.numberOfTapsRequired = 2;

        [imgView addGestureRecognizer:dblTap];

        4.1 点击手势响应的方法

        -(void)onceTapped{

        NSLog(@"onceTapped");

        }

        -(void)twiceTapped{

        NSLog(@"twiceTapped");

        }

     5.Pan手势 --> 拖拽

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewPanned:)];

        [imgView addGestureRecognizer:pan];

        5.1 拖拽时响应的方法

        -(void)viewPanned:(UIPanGestureRecognizer *)pan{

        先记录一下起始位置

        if (pan.state == UIGestureRecognizerStateBegan) {

        originalCenter = imgView.center;

        }

        CGPoint transPoint = [pan translationInView:_imgView];

        CGPoint center = originalCenter;

        center.x += transPoint.x;

        center.y += transPoint.y;

        imgView.center = center;

        CGPoint velocityPoint = [pan velocityInView:_imgView];

        }

    6. LongPress手势 --> 长时间按压

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self  action:@selector(longPressed )];

        [imgView addGestureRecognizer:longPress];

        longPress.minimumPressDuration = 1;

        6.1 长时间按压响应的方法

        -(void)longPressed{

        NSLog(@"long Press");

        }

    7.  Pinch手势 --> 捏合手势

        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchImg:)];

        [imgView addGestureRecognizer:pinch];

        7.1 捏合时响应的方法

        -(void)pinchImg:(UIPinchGestureRecognizer *)pinch {

        if (pinch.state == UIGestureRecognizerStateBegan) {

        originalTrans = _imgView.transform;

        }

        _imgView.transform = CGAffineTransformScale(originalTrans, pinch.scale, pinch.scale);

        }

    8. rotation手势 --> 旋转

        UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateImg:)];

        [imgView addGestureRecognizer:rotation];

     8.1 旋转手势响应的方法

        -(void)rotateImg:(UIRotationGestureRecognizer *)rotation{

        if (rotation.state == UIGestureRecognizerStateBegan) {

        originalTrans = _imgView.transform;

        }

        _imgView.transform = CGAffineTransformRotate(originalTrans, rotation.rotation);

        if (rotation.state == UIGestureRecognizerStateEnded) {

        _imgView.transform = originalTrans;

        }

     }

  • 相关阅读:
    BZOJ_1196_[HNOI2006]公路修建问题_kruskal+二分答案
    BZOJ_1179_[Apio2009]Atm_tarjan+spfa
    BZOJ_1026_[SCOI2009]windy数_数位DP
    BZOJ_1003_[ZJOI2006]物流运输_最短路+dp
    吴裕雄--天生自然HADOOP操作实验学习笔记:tf-idf算法
    吴裕雄--天生自然HADOOP操作实验学习笔记:hive安装
    吴裕雄--天生自然HADOOP操作实验学习笔记:hdfs分布式文件系统安装
    吴裕雄--天生自然HADOOP操作实验学习笔记:单节点伪分布式安装
    吴裕雄--天生自然HADOOP操作实验学习笔记:安装zookeeper集群
    吴裕雄--天生自然HADOOP操作实验学习笔记:hadoop框架认识以及基本操作
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/5398241.html
Copyright © 2020-2023  润新知