进击的KFC:UI(五)手势识别器

本文介绍了iOS中UIGestureRecognizer及其7个子类的具体功能与使用方法,包括轻拍、平移、长按、轻扫、缩放、旋转和边缘扫手势,并提供了详细的代码示例。

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

一.什么是⼿势识别器?
手势类:UIGestureRecognizer是一个抽象类 , 其具体功能交给子类去实现.

二.⼿势识别器的分类
手势识别器有7个子类:
分别识别:轻拍手势,平移手势,长按手势,轻扫手势,缩放手势,旋转手势,边缘扫手势
一旦指定的手势被识别我们可以执行我们自己定义好的操作
这里写图片描述

三.如何使⽤识别器
我们不会直接使⽤⼿势识别器这个抽象⽗类,⽽是根据需要使⽤特定的⼿势识别器创建对象。
1、创建UIxxxGestureRecognizer对象,使⽤initWithTarget:action:⽅法;
2、配置要识别的⼿势的相关信息;
3、将⼿势添加到某个视图上;
4、实现⼿势识别器⾥定义的⽅法

1.创建一个imageView来添加手势
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubView:imageView];
// 开启imageView的交互 (之前提过UIImageView和UILabel的交互默认是关闭的)
imageView.userInteractionEnabled = YES;

(1)轻拍手势:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
// 设置需要点击的次数 (默认是点1次)
tap.numberOfTapRequired = 2;
// 设置几根手指 (默认是一根)
tap.numberOfTouchesRequired = 2;
//把手势添加到视图上
[imageView addGestureRecognizer:tap];

// 方法实现:
- (void)taAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");
}

(2)长按

UILongPressGestureRecognizer *longPress = [[UILongPressRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// 设置长按的时间 (默认是0.5秒)
longPress.minimumPressDuration = 1;
// 设置移动时间
longPress.allowableMovement = 5;
[imageView addGesture:longPress];

// 实现方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
 {
        // 注意,进入长按是一瞬间的事,进入长按状态后无需在调用次方法
        // 所以要对状态进行一次判断
        if(longPress.state == UIGestureRecognizerStateBegin){
            NSLog(@"长按");
   }
}

(3)旋转

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];

// 实现方法
 - (void)rotationAction:(UIRotationGestureRecognizer *)rotation
 {
       // 手势.view :  表示取出 被添加了 手势 的 视图 (view是手势类的一个属性)
       // 形变:transform
       // 参数1:视图.transform
       // 参数2:根据弧度去创建; 用旋转手势自带的弧度属性rotation
       rotation.view.transform = CGAffineTransformRotate(rotation.view.transform,rotation.rotation);
       // 注意:需要每次 把 旋转角度 重置为0.
       // 因为要接替上一次的角度 开始旋转
       rotation.rotation = 0;
       NSLog(@"旋转");
}

(4)捏合

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];

// 实现方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
 {
      // 是根据scale 比例来形变的
      pinch.view.transform = CGAffineTransformScale(pinch.view.transform,pinch.scale,pinch.scale);
      // 重置捏合的比例为:1
      pinch.scale = 1;
      NSLog(@"捏合");
}

(5)平移

 // 平移手势
   UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
   [self.view addGestureRecognizer:pan];
// 实现方法
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    // 获取平移的点 (相对于要平移的视图)
    CGPoint p = [pan translationInView:pan.view];
    // 根据这个点来改变形变属性
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform,p.x,p.y);
    // 重置这个点的初值
    [pan setTranslation:CGPointMake:(0,0) inView:pan.view];
    NSLog(@"平移");
}

(6)轻扫

 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    [self.view addGestureRecognizer:swipe];
    // 设置清扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 实现轻扫方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{ 
    NSLog(@"轻扫");
}

(7)边缘扫

    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)];
    [self.view addGestureRecognizer:edgePan];
    edgePan.edges = UIRectEdgeLeft; // 从手机左侧边缘向右扫
 // 实现方法
 - (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)edgePan
{  
    // 一定是靠近屏幕边缘开始扫,才能触发
    NSLog(@"哎呀,边缘扫啦");  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值