zoukankan      html  css  js  c++  java
  • UIGestureRecognizer

    1、tapGesture 点击手势

    • 1.1 tapGesture 的创建
    // 实例化点击手势对象
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    
    // 向 imageView 添加点击手势
    [imageView addGestureRecognizer:tapGesture];
    
    • 1.2 tapGesture 的设置
    // 设置点击次数
    /*
    默认为 1:单击,为 2 时为双击
    */
    singleTapGesture.numberOfTapsRequired = 1;
    
    // 设置触摸点数
    /*
    默认为 1,单个手指触摸
    */
    singleTapGesture.numberOfTouchesRequired = 1;
    
    // 单双击共存
    /*
    设置单击手势与双击手势共存,当没有检测到双击手势或检测双击手势失败时单击手势才有效
    */
    [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
    
    // 获取点击的视图
    /*
    剪取点击的视图,附带点击手势一起剪取
    */
    UIView *tapView = tapGesture.view;
    
    // 获取点击的图片
    /*
    复制点击的图片,附带点击手势一起复制
    */
    UIImage *tapImage = ((UIImageView *)tapGesture.view).image;
    
    • 1.3 自定义触摸响应事件处理
    - (void)tapClick:(UITapGestureRecognizer *)tapGesture {
    
    }
    

    2、longPressGesture 长按手势

    • 2.1 longPressGesture 的创建
    // 实例化长按手势对象
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
    
    // 向 imageView 添加长按手势
    [imageView addGestureRecognizer:longPressGesture];
    
    • 2.2 longPressGesture 的设置
    // 获取手势状态
    UIGestureRecognizerState state = longPressGesture.state;
    
    // 长按手势开始
    if (longPressGesture.state == UIGestureRecognizerStateBegan) {
    
    }
    
    // 长按手势触发结束
    if (longPressGesture.state == UIGestureRecognizerStateEnded) {
    
    }
    
    • 2.3 自定义触摸响应事件处理
    - (void)longPressClick:(UILongPressGestureRecognizer *)longPressGesture {
    
    }
    

    3、rotationGesture 旋转手势

    • 3.1 rotationGesture 的创建
    // 实例化旋转手势对象
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];
    
    // 向 imageView 添加旋转手势
    [imageView addGestureRecognizer:rotationGesture];
    
    • 3.2 rotationGesture 的设置
    // 获取旋转角度
    /*
    rotation 获取到的为弧度,1 度 = PI/180 弧度
    */
    CGFloat rotation = rotationGesture.rotation  * 180 * M_1_PI;
    
    // 图片旋转
    /*
    lastRotation 为之前的角度
    */
    imageView.transform = CGAffineTransformMakeRotation(lastRotation + rotationGesture.rotation);
    
    // 旋转手势触发结束
    if (rotationGesture.state == UIGestureRecognizerStateEnded) {
    
    	lastRotation += rotationGesture.rotation;
    }
    
    • 3.3 自定义触摸响应事件处理
    // 用模拟器时需按住 option 键
    - (void)rotationClick:(UIRotationGestureRecognizer *)rotationGesture {
    
    }
    

    4、pinchGesture 捏合手势

    • 4.1 pinchGesture 的创建
    // 实例化捏合手势对象
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)];
    
    // 向 imageView 添加捏合手势
    [imageView addGestureRecognizer:pinchGesture];
    
    • 4.2 pinchGesture 的设置
    // 获取缩放倍数
    CGFloat scale = pinchGesture.scale;
    
    // 图片缩放
    imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
    imageView.bounds = CGRectMake(0, 0, imageView.bounds.size.width * pinchGesture.scale, 
    imageView.bounds.size.height * pinchGesture.scale);
    
    // 还原缩放倍数
    [pinchGesture setScale:1];
    
    • 4.3 自定义触摸响应事件处理
    // 用模拟器时需按住 option 键
    - (void)pinchClick:(UIPinchGestureRecognizer *)pinchGesture {
    
    }
    

    5、panGesture 拖动手势

    • 5.1 panGesture 的创建
    // 实例化拖拽手势对象
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
    
    // 向 imageView 添加拖拽手势
    [imageView addGestureRecognizer:panGesture];
    
    • 5.2 panGesture 的设置
    // 获取手势位置
    CGPoint currentPoint = [panGesture locationInView:self.view];
    
    // 图片移动
    imageView.center = currentPoint;
    
    • 5.3 自定义触摸响应事件处理
    - (void)panClick:(UIPanGestureRecognizer *)panGesture {
    
    }
    

    6、swipeGesture 滑动手势

    • 6.1 swipeGesture 的创建
    // 实例化滑动手势对象
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
    
    // 设置滑动方向,默认为 0:向右滑动
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    
    // 向 imageView 添加拖拽手势
    [imageView addGestureRecognizer:swipeGesture];
    
    • 6.2 swipeGesture 的设置
    // 获取滑动方向
    UISwipeGestureRecognizerDirection direction = swipeGesture.direction;
    
    • 6.3 自定义触摸响应事件处理
    - (void)swipeClick:(UISwipeGestureRecognizer *)swipeGesture {
    
    }
    

    7、旋转+捏合+拖拽

    • 7.1 多手势的创建
    // 默认情况下,控件只能监听一种手势
    // 如果要监听到多个手势,设置代理方法,告知允许多个手势存在
    
    // 旋转
    UIRotationGestureRecognizer *rotaitonGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationView:)];
    rotaitonGest.delegate =self;
    [self.imgView addGestureRecognizer:rotaitonGest];
    
    // 捏合
    UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchView:)];
    [self.imgView addGestureRecognizer:pinchGest];
    
    // 拖拽
    UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];
    [self.imgView addGestureRecognizer:panGest];
    
    • 7.2 代理实现
    /**
     *  多个手势同时存在的代理
     */
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        
        return YES;
    }
    
    • 7.3 事件处理
    - (void)rotationView:(UIRotationGestureRecognizer *)rotationGest { 
        
        // 旋转角度
        // 旋转的角度也一个累加的过程
        NSLog(@"旋转角度 %f",rotationGest.rotation);
        
        // 设置图片的旋转
        self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, rotationGest.rotation);
        
        // 清除 "旋转角度" 的累
        rotationGest.rotation = 0;
        
    }
    
    - (void)pinchView:(UIPinchGestureRecognizer *)pinchGest {
    
        // 设置图片缩放
        self.imgView.transform = CGAffineTransformScale(self.imgView.transform, pinchGest.scale, pinchGest.scale);
        
        // 还原
        pinchGest.scale = 1;
    }
    
    - (void)panView:(UIPanGestureRecognizer *)panGest {
        
        // 拖拽的距离(距离是一个累加)
        CGPoint trans = [panGest translationInView:panGest.view];
        NSLog(@"%@",NSStringFromCGPoint(trans));
        
        // 设置图片移动
        CGPoint center =  self.imgView.center;
        center.x += trans.x;
        center.y += trans.y;
        self.imgView.center = center;
        
        // 清除累加的距离
        [panGest setTranslation:CGPointZero inView:panGest.view];
    }
    
    
  • 相关阅读:
    HTML+CSS笔记 CSS进阶续集
    HTML+CSS笔记 CSS进阶
    HTML+CSS笔记 CSS入门续集
    HTML+CSS笔记 CSS入门
    test
    Python Paramiko模块安装和使用
    RedHat升级Python到2.7.6
    python数据库操作常用功能使用详解(创建表/插入数据/获取数据)
    5、使用EF对后台SysSample数据增删改查
    4、创建SQL数据库,添加EF实体数据模型
  • 原文地址:https://www.cnblogs.com/CH520/p/9413507.html
Copyright © 2011-2022 走看看