手势的理解:
首先有一个父类UIGestureRecognizer是虚拟类,他的几个子类是我们需要使用到的常见手势,跳进文档里面
Overview
The concrete subclasses of UIGestureRecognizer are the following:
• UITapGestureRecognizer
• UILongPressGestureRecognizer
• UISwipeGestureRecognizer
• UIPinchGestureRecognizer
• UIRotationGestureRecognizer
• UIPanGestureRecognizer
• UIScreenEdgePanGestureRecognizer
- 轻敲手势Tap
- 长按手势:LongPress
- 轻扫手势:swipe
- 捏合:pinch
- 旋转:Rotation
- 拖拽,吸附Pan
- 暂无实际用处UIPanGestureRecognizer的子类.边缘拖动的意思
属性
加手势,怎么加?UIView就有直接加这些手势的方法
** UIView (UIViewGestureRecognizers)分类,所以控件都可以使用 **
1. 添加手势
addGestureRecognizer:
2. 移除手势
removeGestureRecognizer
3. 是否接收一个手势触摸事件,默认为YES,返回NO为不接收
gestureRecognizerShouldBegin
state 状态(很多种状态,从开始到结束)
enabled 是否有效
view 触摸的视图
cancelsTouchesInView 取消触摸控件的响应.
delaysTouchesBegan. 触摸事件延时开始
UIGestureRecognizerDelegate(重点)
方法
initWithTarget 给手势对象添加事件
removeTarget 移除事件
requireGestureRecognizerToFail 多个手势时如何触发
// 关键在这一行,如果双击确定偵測失败才會触发单击
[singleRecognizer requireGestureRecognizerToFail:doubleRecognizer]
locationInView 触摸点坐标
numberOfTouches 触摸点数
locationOfTouch 多指触摸的触摸点相对于指定视图的位置
代理
1. gestureRecognizerShouldBegin:开始手势识别
2. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
例子:UITapGestureRecognizer和UIButton的点击事件冲突的解决办法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isKindOfClass:[UIButton class]]) {
return NO; }
return YES;
}
3. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;同上表示按压
4. 是否支持多手势触发
shouldRecognizeSimultaneouslyWithGestureRecognizer
使用
代码部分
< 1. UITapGestureRecognizer两个属性
1、numberOfTapsRequired;
2、numberOfTouchesRequired;
// 设置能识别到手势的最少的轻触次数
tap.numberOfTapsRequired = 3;
// 设置能识别到手势的最少的手指的个数
tap.numberOfTouchesRequired = 2;
< 2. UILongPressGestureRecognizer
1、numberOfTapsRequired;
2、numberOfTouchesRequired;
3、minimumPressDuration;
4、allowableMovement;
< 3. UISwipeGestureRecognizer
1、direction方向
< 4. UIRotationGestureRecognizer
1. rotation角度
注意这个手势只是用来监听你的双手旋转的,真的让控件转动还是需要使用transform才行
< 5. UIPanGestureRecognizer
1. 方法:translationInView可以获取手势的点
< 6.UIPinchGestureRecognizer
1. scale缩放比例
用法都是大同小异没啥特点的