IOS 手势、手势响应器UIGestureRecognizer

手势,手势响应GestureRecognizer

关于手势响应IOS中封装了一个类,能响应一般手势UIGestureRecognizer

下面说一下这个UIGestureRecognizer 类

这个类有几个子类,这几个子类分别表示几种不同的基本手势

1、UITapGestureRecognizer //点击手势识别器,可以是点击一次,或多次都能识别

2、UIPinchGestureRecognizer //捏合手势识别器,用于视图的放大缩小

3、UIRotationGestureRecognizer //旋转手势识别器

4、UISwipeGestureRecognizer //滑动手势识别器,向上、下、左、右滑动

5、UIPanGestureRecognizer //拖动手势识别器

6、UILongPressGestureRecognizer //长按手势识别器,常见的有长按跳出一个界面用以编辑

首先说一下手势用法的基本流程

1、创建手势识别器,这样才能获取手势识别器能处理的手势

2、配置能识别的具体手势,如是能识别向左滑还是相右滑

3、把手势识别器添加到视图中

一、首先是点击手势

其实点击分为两种情况一种是单击,另一种是多击(一般只用到双击)

下面就创建一个能响应单击手势的识别器

//创建点击手势识别器

UITapGestureRecognizer *pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];

//设置这个点击手势识别器所能响应的点击次数,单击所以设成1

pSingleTap.numberOfTapsRequired = 1;

//添加到视图中

[self.view addGestureRecognizer:pSingleTap];

其中singleGesture:这个就是当手势识别器识别了手势后所调用的方法,

这个方法是自己写的,用于处理响应后的操作

这样就三话就创建了一个可以识别单击的识别器。很方便

下面再创建一个双击手势识别器,跟单击基本上一样,只是多了一行代码

//创建双击

UITapGestureRecognizer *pDoubuleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];

pDoubuleTap.numberOfTapsRequired = 2;

[self.view addGestureRecognizer:pDoubuleTap];

[pSingleTap requireGestureRecognizerToFail:pDoubuleTap];

在这里为什么要加上最后一行代码呢?

如果不加最后一行代码的话,当你双击的时候虽然双击手势识别器识别了这个手势,

并且调用方法doubleGesture:响应了双击手势,但是问题是,在响应这个手势的时候

单击手势也再响应,并响应了两次。

正式为了解决这个问题所以才有了最后一行代码,下面说一下这行代码到底什么意思

首先看这个方法的英文就可以大概得出:手势识别器pSingleTap 是在pDoubuleTap识别失败的时候才识别的。

也就是说当有一个点击手势时首先查看这个手势是否是pDoubuleTap能识别处理的手势,若pDoubuleTap能

识别这个手势,那么就不再请求pSingleTap 来识别,相反,若pDoubuleTap不能识别,则再请求看pSingleTap

能不能识别,这样就分开了对这个手势的双重响应

那要是再加一个三击的手势识别器呢,只需把最后一句改一下就行

[pDoubuleTap requireGestureRecognizerToFail:pThreeTap];

相信应该很好理解了,先看pThreeTap能不能识别,不能识别再看pDoubuleTap能不能识别,最后

pDoubuleTap不能识别的话再请求pSingleTap手势识别器来识别

点击就说这么多吧

二、滑动手势UISwipeGestureRecognizer //滑动手势识别器,向上、下、左、右滑动

下面创建一个滑动手势识别器

//清扫

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];

//设置能识别滑动手势的方向,

swipe.direction = UISwipeGestureRecognizerDirectionLeft;

swipe.direction = UISwipeGestureRecognizerDirectionRight;

swipe.direction = UISwipeGestureRecognizerDirectionUp;

swipe.direction = UISwipeGestureRecognizerDirectionDown;

//注意一个手势识别器只能识别一个方向上的滑动

[self.view addGestureRecognizer:swipe];

三、长按手势UILongPressGestureRecognizer

创建长按手势识别器

长按

UILongPressGestureRecognizer *pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

//设置长按多少秒后才识别这个手势

pLongPress.minimumPressDuration = 2;

[self.view addGestureRecognizer:pLongPress];

四、移动手势UIPanGestureRecognizer

创建

UIPanGestureRecognizer *pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];

[self.view addGestureRecognizer:pPan];

可以根据平移时的位置的移动来拖动视图

获取位置 CGPoint point = [pPan locationInView:self.view];

用这个方法可以获取到当前移动到得位置

五、捏合手势UIPinchGestureRecognizer

捏合有两个属性

@property (nonatomic,readonly) CGFloat velocity; //捏合的速度

@property (nonatomic) CGFloat scale; //比例(经常用到放缩比例)

这个属性默认值是1,通过获取放缩比例属性

创建

//添加捏合手势

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

pinch.delegate = self; //设置委托,这个委托属性是属于UIGestureRecognizer 的

[self.view addGestureRecognizer:pinch];

六、旋转手势UIRotationGestureRecognizer

旋转也由两个属性

@property (nonatomic) CGFloat rotation; //旋转角度

@property (nonatomic,readonly) CGFloat velocity; //旋转速度

这个主要通过获取旋转角度来完成一些列操作

//添加旋转手势识别器

UIRotationGestureRecognizer *rotat = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRotate:)];

rotat.delegate =self;

[self.view addGestureRecognizer:rotat];


demo https://github.com/13662049573/SevenGesture-demo-

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值