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