6种手势的讲解

本文详细介绍了六种常用的手势识别技术,包括轻击、轻扫、长按、拖动、捏合及旋转手势,并提供了具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里主要讲解一下自己理解的关于6种手势的相关内容
六种手势包括:

轻击手势(TapGestureRecognizer),

轻扫手势 (SwipeGestureRecognizer),

长按手势(LongPressGestureRecognizer),

拖动手势(PanGestureRecognizer),

捏合手势(PinchGestureRecognizer),

旋转手势(RotationGestureRecognizer),

(1)、轻击手势(包括单击和双击)
//单击

 {
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [imageV addGestureRecognizer:tap];
    }
    //方法
    -(void)tapAction:(UITapGestureRecognizer*)tap{
    NSLog(@"单击");   
}

//双击

{
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [imageV addGestureRecognizer:doubleTap];
    //双击失败了才算单击(双击成功就是双击)
    [tap requireGestureRecognizerToFail:doubleTap];
    }
-(void)doubleTapAction:(UITapGestureRecognizer*)tap{
    NSLog(@"双击");
}

(2)轻扫手势

{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    //属性:设置轻扫的方向(默认 右)
//swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [imageV addGestureRecognizer:swipe];
}
-(void)swipeAction:(UISwipeGestureRecognizer*)swipe{
    NSLog(@"轻扫");
}

(3)长按手势

{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [imageV addGestureRecognizer:longPress];
    //其中属性allowableMovement:是允许抖动的范围
}
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
    NSLog(@"长按开始");
    }else if (longPress.state ==UIGestureRecognizerStateEnded){
        NSLog(@"长按结束");
    }    
}

(4)拖动手势

{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [imageV addGestureRecognizer:pan];
    //轻扫和移动的冲突,一旦有冲突,默认移动
    //轻扫失败算移动  (轻扫成功就是轻扫)
    [pan requireGestureRecognizerToFail:swipe];
}
-(void)panAction:(UIPanGestureRecognizer*)pan{

//    NSLog(@"移动...");
    //设置当前位置的坐标(相对 起始点 )
     CGPoint p1 =[pan translationInView:imageV];
    NSLog(@"p1 = %@",NSStringFromCGPoint(p1));

    //移动点的速度
//    CGPoint v1 = [pan velocityInView:imageV];
//    NSLog(@"v1 = %@",NSStringFromCGPoint(v1));

    //设置当前位置
//    CGPoint p2 = CGPointMake(100, 100);
//    [pan setTranslation:p2 inView:imageV];
}

(5)捏合手势

{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [imageV addGestureRecognizer:pinch];
}
-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{

    /*属性
     scale:缩放的大小比例
     velocity:缩放的速度
     */
    if (pinch.state ==UIGestureRecognizerStateChanged) {
        NSLog(@"一直捏合");
        CGFloat pScale  =pinch.scale;
        pinch.view.transform = CGAffineTransformMakeScale(pScale, pScale);   
    }else if (pinch.state == UIGestureRecognizerStateEnded){
        NSLog(@"捏合结束");
        [UIView animateWithDuration:0.2 animations:^{
                    pinch.view.transform =CGAffineTransformIdentity;
        }];
    }
}

(6)旋转手势

{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [imageV addGestureRecognizer:rotation];
}
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{

    /*
     rotation:旋转的弧度 角度:0~360   弧度:0~2π
     velocity:旋转的速度
     */
    if (rotation.state == UIGestureRecognizerStateChanged) {
        NSLog(@"一直旋转");    
        CGFloat angle = rotation.rotation;     
        rotation.view.transform = CGAffineTransformMakeRotation(angle);     
    }else if (rotation.state == UIGestureRecognizerStateEnded){
        NSLog(@"旋转结束");
        [UIView animateWithDuration:0.2 animations:^{
                    rotation.view.transform = CGAffineTransformIdentity;
        }];
    }
}

在模拟器上进行手势操作时可能会出现错误,建议尽量在真机上进行操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值