UIGestureRecognizer手势识别器

监听触摸事件的做法

• 如果想监听一个view上面的触摸事件, 一个做法
    ➢ 自定义⼀个view
    ➢ 实现view的touches方法,在方法内部实现具体处理代码

• 通过touches方法监听view触摸事件,有很明显的几个缺点
    ➢ 必须得自定义view
    ➢ 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
    ➢ 不容易区分用户的具体手势行为

另一个做法是手势识别器
• iOS 3.2之后,苹果推出了⼿势识别功能(Gesture Recognizer),在触摸事件处理⽅面,⼤大简化了开发者的开发难度

• 为了完成手势识别,必须借助于手势识别器---- UIGestureRecognizer
• 利⽤用UIGestureRecognizer,能轻松识别⽤用户在某个view上面做的一些常见手势
• UIGestureRecognizer是⼀一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势

➢ UITapGestureRecognizer(敲击)
➢ UIPinchGestureRecognizer(捏合,⽤用于缩放)
➢ UIPanGestureRecognizer(拖拽)
➢ UISwipeGestureRecognizer(轻扫)
➢ UIRotationGestureRecognizer(旋转)
➢ UILongPressGestureRecognizer(长按)


手势识别器的用法
• 每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下
➢ 创建手势识别器对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
➢ 设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
➢ 添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap];
➢ 监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];


手势识别的状态
typedef NS_ENUM(NSInteger,
UIGestureRecognizerState) {
// 没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
// 一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
// 手势状态改变
UIGestureRecognizerStateChanged,
// ⼿势完成
UIGestureRecognizerStateEnded,
// ⼿势取消恢⾄Possible状态
UIGestureRecognizerStateCancelled
}

 
stroyboardAndTest


//
//  ViewController.m
//  gestureRecognizer
//
//  Created by ys on 15/11/21.
//  Copyright (c) 2015年 ys. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    /**
     手势识别器新建->添加->监听
     */
//    //新建手势识别器,这些识别器均继承自UIGestureRecognizer
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
//    //将手势识别器添加到view
//    [self.imageView addGestureRecognizer:tap];
//    //监听手势识别器
//    [tap addTarget:self action:@selector(tapAction)];
    
    /*
     *点击识别器
     */
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)]];
    
    
    /*
     *长按识别器
     */
    [self.imageView addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction)]];
    
    
    /*
     *滑动识别器
     */
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
    /*
     *滑动方向
     */
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipe];
    
    
    /*
     *旋转识别器
     */
    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    rotate.delegate = self;
    [self.imageView addGestureRecognizer:rotate];

    
    /**
     *  缩放识别器
     */
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    pinch.delegate = self;
    [self.imageView addGestureRecognizer:pinch];

    
    /**
     *移动识别器
     */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.imageView addGestureRecognizer:pan];
    
}




-(void)tapAction
{
    NSLog(@"tap------");
}


-(void)longPressAction
{
    NSLog(@"longPress------");
}


-(void)swipeAction
{
    NSLog(@"swipe----left");
}


-(void)rotationAction:(UIRotationGestureRecognizer *)rotate
{
    rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
    /**
     *  清零rotation可以解决旋转度数累积(数据冗余)造成旋转速度过快的bug
     */
    rotate.rotation = 0;
}


-(void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;//同rotation一样解决数据冗余问题
}


-(void)panAction:(UIPanGestureRecognizer *)pan
{
    CGPoint point = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += point.x;
    center.y += point.y;
    pan.view.center = center;
    [pan setTranslation:CGPointZero inView:pan.view];//清零位移
}




/**
 *  手势识别器代理方法shouldRecognizeSimultaneouslyWithGestureRecognizer
 *
 *  @return 是否可以两个识别器同时发生作用,此demo同时监听旋转和缩放
 */
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值