iOS开发概述-12.手势处理

1.手势识别概述

  • 如果想监听一个view上面的触摸事件,之前的做法是
    • 自定义一个view
    • 实现view的touches方法,在方法内部实现具体处理代码
  • 但是通过touches方法监听view触摸事件,有很明显的几个缺点
    • 必须得自定义view
    • 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
    • 不容易区分用户的具体手势行为
    • iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度

2.UIGestureRecognizer(手势识别器)

  • 为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer
  • 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
  • UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
    • UITapGestureRecognizer(敲击)
    • UIPinchGestureRecognizer(捏合,用于缩放)
    • UIPanGestureRecognizer(拖拽)
    • UISwipeGestureRecognizer(轻扫)
    • UIRotationGestureRecognizer(旋转)
    • UILongPressGestureRecognizer(长按)

3.手势识别器的基本使用

  • UITapGestureRecognizer(点击手势)
#pragma mark - 添加点击手势
// 设置敲击手势
-(void)setUpTap{
    // 敲击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    // 连续敲击两次
//    tap.numberOfTapsRequired = 2;
    // 需要两个手指一起敲击
//    tap.numberOfTouchesRequired = 2;
    // 设置手势代理,实现左半边可以点击,右半边不能点击
    tap.delegate = self;
    // 添加手势
    [self.redView addGestureRecognizer:tap];
}
#pragma mark -UIGestureRecognizerDelegate
// 是否可以接受触摸事件
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 获取当前触摸点的位置
    CGPoint curP = [touch locationInView:self.redView];
    // 判断触摸点的位置
    if (curP.x > self.redView.bounds.size.width * 0.5) { // 触摸位置在右半部分
        return NO;
    }else{// 触摸位置在左半部分
        return YES;
    }
}
// 敲击手势识别方法
-(void)tap:(UITapGestureRecognizer *)tap{
    NSLog(@"敲击手势");
}
  • UIPinchGestureRecognizer(捏合手势)
#pragma mark - 添加捏合手势
// 添加捏合手势
-(void)setUpPin{
    // 创建捏合手势
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];

    [self.redView addGestureRecognizer:pin];
}
// 捏合手势实现方法
-(void)pin:(UIPinchGestureRecognizer *)pin{
    // 根据捏合比例,相对上一次的视图尺寸进行放大缩小
    self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);
    // 复位,设定捏合后的视图尺寸为1
    pin.scale = 1.0;
}
  • UISwipeGestureRecognizer(轻扫手势)
#pragma mark - 添加左右轻扫手势
// 添加轻扫手势
-(void)setUpSwipe{
    // 创建轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 设置轻扫手势方向,一个手势只能添加一个方向
    /*
     typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
     UISwipeGestureRecognizerDirectionRight = 1 << 0, 右边
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1, 左边
     UISwipeGestureRecognizerDirectionUp    = 1 << 2, 上边
     UISwipeGestureRecognizerDirectionDown  = 1 << 3  下边
     };
     */
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.redView addGestureRecognizer:swipe];
    // 添加新的手势(向右轻扫)
    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe1.direction = UISwipeGestureRecognizerDirectionRight;
    [self.redView addGestureRecognizer:swipe1];
}
// 轻扫手势实现方法
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
    // 判断手势方向
    if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
        NSLog(@"向左扫");
    }else if(swipe.direction == UISwipeGestureRecognizerDirectionRight){
        NSLog(@"向右扫");
    }
}

  • 同时添加旋转和捏合手势
#pragma mark - 添加旋转+捏合手势
// 添加旋转手势
-(void)setUpRotation{
     // 旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    // 添加手势
    [self.redView addGestureRecognizer:rotation];
    // 添加捏合手势
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];
    pin.delegate = self;
    [self.redView addGestureRecognizer:pin];

}
// 旋转执行方法
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
    // 将弧度转换为角度
    self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotation.rotation);
    // 复位
    rotation.rotation = 0;
}
// 捏合手势实现方法
-(void)pin:(UIPinchGestureRecognizer *)pin{
    // 根据捏合比例,相对上一次的视图尺寸进行放大缩小
    self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);
    // 复位,设定捏合后的视图尺寸为1
    pin.scale = 1.0;
}
#pragma mark -UIGestureRecognizerDelegate
 //可以同时识别多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
#pragma mark -UIGestureRecognizerDelegate
// 代理方法,可以让视图同时识别多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
  • UILongPressGestureRecognizer(长按手势)
#pragma mark - 添加长安手势
-(void)setUpPress{
    // 创建长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    // 设置长按手势的最小停留时间(系统默认是0.5s)
    longPress.minimumPressDuration = 1.0;
    // 设置长按允许滑动的距离(系统默认是10,一旦手势停留时间超出最小停留时间,限制无效)
    longPress.allowableMovement = 5;
    // 添加长按手势
    [self.redView addGestureRecognizer:longPress];
}

// 长按手势监听方法实现
-(void)longPress:(UILongPressGestureRecognizer *)longPress{

    NSLog(@"长按手势");
}
  • UIPanGestureRecognizer(拖拽手势)
#pragma mark - 添加拖拽手势
-(void)setUpPan{
    // 创建手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    // 添加手势
    [self.redView addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
    // 获取拖拽的偏移位置
    CGPoint curP = [pan translationInView:self.redView];
    // 根据偏移位置形变
    self.redView.transform  = CGAffineTransformTranslate(self.redView.transform, curP.x, curP.y);
    // 复位,让下一次拖拽拿到的偏移位置是相对于现在的位置
    [pan setTranslation:CGPointZero inView:self.redView];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值