通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类)。
PS:自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubclass.h>,一般需实现如下方法:
1 - (void)reset; 2 3 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 4 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 5 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 6 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 7 //以上方法在分类 UIGestureRecognizer (UIGestureRecognizerProtected) 中声明,更多方法声明请自行查看
手势状态枚举如下:
1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { 2 UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态 3 UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成 4 UIGestureRecognizerStateChanged, // 手势状态发生转变 5 UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指) 6 UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态 7 UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态 8 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded 9 };