美文网首页
UIGestureRecognizer - 手势

UIGestureRecognizer - 手势

作者: 居然是村长 | 来源:发表于2016-04-04 11:29 被阅读84次

    UIGestureRecognizer 抽象类

        tap.cancelsTouchesInView = NO;
        tap.delaysTouchesBegan = NO;
        tap.delaysTouchesEnded = YES;
        
        tap.delegate = self;// 有多个代理,判断手势的可用性等。
    
    
        // 识别手势后,可以获取手势状态等 做相应的处理
        UIGestureRecognizerState state = gesture.state;
    
        NSUInteger num = [gesture numberOfTouches];
        CGPoint point = [gesture locationInView:self.testView];// 点击位置
        CGPoint point2 = [gesture locationInView:self.view];// 点击位置
        CGPoint point3 = [gesture locationInView:gesture.view];// 点击位置,手势自带view
        
    ...
    
    

    UITapGestureRecognizer 点击手势

        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
        gesture.numberOfTapsRequired = 1;// 点击次数
        gesture.numberOfTouchesRequired = 1;// 手指数
        [self.testView addGestureRecognizer:gesture];
    

    UILongPressGestureRecognizer 长按

        UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
        gesture.numberOfTapsRequired = 0;// 默认要 0 
        gesture.numberOfTouchesRequired = 1;// 手指
        gesture.minimumPressDuration = .5;// 触发时间
        gesture.allowableMovement = 10.;// 触发允许移动范围
    
    

    UISwipeGestureRecognizer 轻扫

        UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
        gesture.numberOfTouchesRequired = 1;// 手指
        gesture.direction = UISwipeGestureRecognizerDirectionRight;// 方向,多选
        
    
    

    UIPinchGestureRecognizer 捏合(缩放)

        UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
    // 缩放比例,和缩放速度,可用与控制判断
    
    
    // 举例
    - (void)test:(UIPinchGestureRecognizer *)gesture { 
        if (gesture.state == UIGestureRecognizerStateChanged) {
            //捏合手势中scale属性记录的缩放比例
            if (gesture.scale < 2) {
                gesture.view.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
            }
        }
        
        //结束后恢复
        if(gesture.state==UIGestureRecognizerStateEnded) {
            [UIView animateWithDuration:0.5 animations:^{
                gesture.view.transform = CGAffineTransformIdentity;//取消一切形变
            }];
        }
    }
    

    UIPanGestureRecognizer 拖动

        UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
        gesture.minimumNumberOfTouches = 1;
        gesture.maximumNumberOfTouches = 1;
    
    
    // 举例
    - (void)test:(UIPanGestureRecognizer *)gesture {
        CGPoint tarnslatePoint = [gesture translationInView:gesture.view];// 具体位置
        CGPoint velocityPoint = [gesture velocityInView:gesture.view];// 每次的偏移量
        
        if (gesture.state == UIGestureRecognizerStateChanged) {
            gesture.view.transform = CGAffineTransformMakeTranslation(tarnslatePoint.x, tarnslatePoint.y);
        } 
    
        if (gesture.state == UIGestureRecognizerStateEnded) {
            gesture.view.transform = CGAffineTransformIdentity;
        }
    }
    

    UIRotationGestureRecognizer 旋转

        UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(test:)];
    // 旋转角度,和旋转速度,可以用于控制判断
     
    - (void)test:(UIRotationGestureRecognizer *)gesture {
        if (gesture.state==UIGestureRecognizerStateChanged) {
            gesture.view.transform=CGAffineTransformMakeRotation(gesture.rotation);
        }
        
        if(gesture.state==UIGestureRecognizerStateEnded) {
            gesture.view.transform=CGAffineTransformIdentity;
        }
    }
    

    其他

    手势抽象类,以及其子类还有更多的方法,待考究。
    上面结合 CGAffineTransform 可以看看另外的一篇内容简单介绍。http://www.jianshu.com/p/7b83bb32c74a

    1

    相关文章

      网友评论

          本文标题:UIGestureRecognizer - 手势

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