美文网首页iOS Developer
iOS手势识别器

iOS手势识别器

作者: 光是光光的光呐 | 来源:发表于2016-05-04 09:20 被阅读321次

UIGestureRecognizer

UIGestureRecognizer类,用于检测、识别用户使用设备时所用的手势.它是一个抽象类,定义了所有手势的基本行为.以下是UIGestureRecognizer子类,用于处理具体的用户手势行为:

UITapGestureRecognizer // 1.单击
UILongPressGestureRecognizer // 3.长按
UISwipeGestureRecognizer // 4.轻扫
UIPanGestureRecognizer // 5.移动
UIRotationGestureRecognizer // 6.旋转
UIPinchGestureRecognizer // 7.捏合

创建手势:
    // 1.单击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imgView addGestureRecognizer:tap];
    
    // 2.双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [imgView addGestureRecognizer:doubleTap];
    
    // 双击失败才单击
    [tap requireGestureRecognizerToFail:doubleTap];
    
    // 3.长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //设置最短时间
    longPress.minimumPressDuration = 1;
    [imgView addGestureRecognizer:longPress];
    
    
    // 4.轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // 设置轻扫方向
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [imgView addGestureRecognizer:swipe];
    
    // 5.移动
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imgView addGestureRecognizer:pan];
    
    // 轻扫失败才移动
    [pan requireGestureRecognizerToFail:swipe];
    
    // 6.旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imgView addGestureRecognizer:rotation];
    
    // 7.捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imgView addGestureRecognizer:pinch];
手势触发事件:

GestureAction:

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog(@"长按结束");
    }
}

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

    //手指所在的坐标
    CGPoint point = [pan locationInView:self.view];
    _view.center = point;
}

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

    if (rotation.state == UIGestureRecognizerStateChanged) {
        
        //取到弧度
        CGFloat angle = rotation.rotation;
        
        //正在旋转
        rotation.view.transform = CGAffineTransformMakeRotation(angle);
        
    } else if (rotation.state == UIGestureRecognizerStateEnded) {
        
        //还原
        [UIView animateWithDuration:.5 animations:^{
            
            rotation.view.transform = CGAffineTransformIdentity;
        }];
    }
}

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

    if (pinch.state == UIGestureRecognizerStateChanged) {
        
        // 取到缩放比率
        CGFloat scale = pinch.scale;
        
        // 缩放
        pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
        
    } else if (pinch.state == UIGestureRecognizerStateEnded) {
        
        [UIView animateWithDuration:.5 animations:^{
            
            pinch.view.transform = CGAffineTransformIdentity;
        }];
    }
}
Motion 摇晃手势
//让当前对象成为第一响应者
- (BOOL)canBecomeFirstResponder
{
          
   return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

   NSLog(@"摇一摇开始");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

   NSLog(@"摇一摇结束");
}

推荐一篇iOS手势识别的详细使用的文章:iOS手势识别

相关文章

  • iOS手势识别器

    1.手势识别器 1.手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。是对触摸事件做...

  • 3.6 iOS手势识别的状态和手势识别器幕后原理

    2.2手势识别的状态和手势识别器幕后原理 (一)手势的状态 (二)离散型手势识别器和连续型手势识别器之间的对比: ...

  • Gesture手势

    手势识别器 手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们...

  • 手势——UIGestureRecognizer

    一、简介 UIGestureRecognizer是具体手势识别器的基类。 手势识别器对象(或简单地说是手势识别器)...

  • UIGestureRecognizer

    什么是手势识别器? 手势识别器就是对触摸事件做了封装,我们不需要判断某个手势是否触发,手势识别器本身起到了识别作用...

  • iOS手势识别

    UIGestureRecognizer手势识别器手势识别器是特殊的触摸事件UIGestureRecognizer是...

  • UIGestureRecognizer手势识别器学习笔记

    UIGestureRecognizer 具体手势识别器的基类。一个手势识别器对象,或者简单地说一个手势识别器,解耦...

  • iOS手势识别器

    UIGestureRecognizer UIGestureRecognizer类,用于检测、识别用户使用设备时所用...

  • iOS手势识别器

    手势识别器是附加到视图的对象,将低级别事件处理代码转换为更高级别的操作,它允许视图以控件执行的方式响应操作。 手势...

  • 手势识别

    手势识别 6种手势识别 在iOS开发中有6中手势识别:点按、捏合、拖动、轻扫、旋转、长按苹果推出手势识别主要是为了...

网友评论

    本文标题: iOS手势识别器

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