zoukankan      html  css  js  c++  java
  • 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;

        }

     }

  • 相关阅读:
    怎么用代码弹回 UITableView 中左滑出来的删除按钮
    android 利用 aapt 解析 apk 得到应用名称 包名 版本号 权限等信息
    Missy
    html5 websocket + node.js 实现网页聊天室
    android 代码混淆示例
    android volley 发送 POST 请求
    android viewpager 拿到当前显示的 fragment 的实例
    android actionbar viewpager 实现类微信主界面布局
    (转)初学Git及简单搭建git服务器和客户端
    error: Cannot find OpenSSL's <evp.h> Mac
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/5398241.html
Copyright © 2011-2022 走看看