- 为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer
- 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
- UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer //敲击
UIPinchGestureRecognizer //捏合,用于缩放
UIPanGestureRecognizer //拖拽
UISwipeGestureRecognizer //轻扫
UIRotationGestureRecognizer //旋转
UILongPressGestureRecognizer //长按
- UITapGestureRecognizer
- 每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下
//创建手势识别器对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]init];
//设置手势识别器对象的具体属性
tap.numberOfTapsRequired = 2; //连续敲击2次
tap.numberOfTouchesRequired = 2; //需要2根手指一起敲击
//添加手势识别器到对应的view上
[self.iconViewaddGestureRecognizer:tap];
//监听手势的触发
[tapaddTarget:selfaction:@selector(tapIconView:)];
- 手势识别的状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, //没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStateBegan, //一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateChanged, //手势状态改变
UIGestureRecognizerStateEnded, //手势完成
UIGestureRecognizerStateCancelled, //手势取消,恢复至Possible状态
UIGestureRecognizerStateFailed, //手势失败,恢复至Possible状态
//识别到手势识别
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
-
手势识别状态变化示意图