在iOS中添加手势比较简单,可以归纳为以下几个步骤:
一、UIGestureRecognizer有六个子类,分别为:
1、UILongPressGestureRecognizer(长按)
2、UIPanGestureRecognizer(拖移)
3、UIPinchGestureRecognizer(捏合)
4、UIRotationGestureRecognizer(旋转)
5、UISwipeGestureRecognizer(滑动)
6、UITapGestureRecognizer(点击)
在这六种手势识别中,只有一种手势是离散手势,它就是UITapGestureRecgnier。离散手势的特点就是一旦识别就无法取消,而且只会调用一次手势操作事件(初始化手势时指定的触发方法)。换句话说其他五种手势是连续手势,连续手势的特点就是会多次调用手势操作事件,而且在连续手势识别后可以取消手势。从下图可以看出两者调用操作事件的次数是不同的:
二、父类UIGestureRecognizer的代理及属性
@protocol UIGestureRecognizerDelegate;
@classUIView, UIEvent, UITouch, UIPress;
//枚举手势的7中状态
typedefNS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
UIGestureRecognizerStateChanged, // 手势状态发生转变
UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)
UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded// 手势识别完成,同UIGestureRecognizerStateEnded
};
备注:苹果官方的分析图有助于理解(上传失败)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject
//创建的时候就添加触摸事件
- (instancetype)initWithTarget:(nullableid)target action:(nullableSEL)actionNS_DESIGNATED_INITIALIZER; // designated initializer
//添加触摸执行事件
- (void)addTarget:(id)target action:(SEL)action;
//移除触摸执行事件
- (void)removeTarget:(nullableid)target action:(nullableSEL)action;
//设置手势的状态
@property(nonatomic,readonly) UIGestureRecognizerState state;
@property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate;
//手势是否可用
@property(nonatomic, getter=isEnabled) BOOL enabled;
//触发手势的视图(一般在触摸执行操作中我们可以通过此属性获得触摸视图进行操作)
@property(nullable, nonatomic,readonly) UIView *view;
@property(nonatomic) BOOL cancelsTouchesInView;
//手势识别失败前不执行触摸开始事件,默认为NO;如果为YES,那么成功识别则不执行触摸开始事件,失败则执行触摸开始事件;如果为NO,则不管成功与否都执行触摸开始事件;
@property(nonatomic) BOOL delaysTouchesBegan;
@property(nonatomic) BOOL delaysTouchesEnded;
@property(nonatomic, copy) NSArray<NSNumber *> *allowedTouchTypesNS_AVAILABLE_IOS(9_0); // Array of UITouchType's as NSNumbers.
@property(nonatomic, copy) NSArray<NSNumber *> *allowedPressTypesNS_AVAILABLE_IOS(9_0); // Array of UIPressTypes as NSNumbers.
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer*)otherGestureRecognizer;
//在指定视图中的相对位置
- (CGPoint)locationInView:(nullableUIView*)view;
//触摸点的个数(就是同时触摸的手指个数)
- (NSUInteger)numberOfTouches;
//触摸点相对于指定视图的位置
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullableUIView*)view; // the location of a particular touch
@end
@protocol UIGestureRecognizerDelegate <NSObject>
@optional
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer;
//一个控件的手势识别后是否阻断手势识别继续向下传播,默认返回NO;如果为YES,响应者链上层对象触发手势识别后,如果下层对象也添加了手势并成功识别也会继续执行,否则上层对象识别后则不再继续传播;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer;
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizerNS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerNS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;
@end