前言:官方文档
概述
UIGestureRecognizer类是用于具体手势识别器的基本类。应用在添加手势识别功能时,需要针对特定的手势创建相对应的UIGestureRecognizer子类对象,而不是直接使用UIGestureRecognizer对象。使用UIGestureRecognizer子类对象的时候,除了要设置目标-动作对,还要将该子类对象添加在某个视图上。当该子类对象根据当前加载到的视图所发生的触摸事件识别出相应的手势时,就会向指定的目标对象发送指定的动作消息。由UIGestureRecognizer对象发出的动作消息遵循以下规范:
- (void)gestureRecognizerDetected:(UIGestureRecognizer *)recognizer;
UIGestureRecognizer对象在识别手势时,会截取本应由其附着的视图自行处理的触摸事件(如下图所示),所有,附着了UIGestureRecognizer对象的视图可能不会接收到常规的UIResponder消息,例如响应触摸事件方法- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event。
UIGestureRecognizer包含以下子类
1、UITapGestureRecognizer(点击手势)
2、UIPinchGestureRecognizer(捏合手势)
3、UIRotationGestureRecognizer(旋转手势)
4、UISwipeGestureRecognizer(滑动手势)
5、UIPanGestureRecognizer(拖动手势)
属性方法
1、初始化识别器
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action NS_DESIGNATED_INITIALIZER;
2、管理手势交互
/*手势识别器委托*/
@property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate;
UIGestureRecognizerDelegate
A、手势识别
/*开始触摸*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
/*是否允许手势识别器检查触摸对象,默认为YES*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
/*是否允许手势识别器检查按压对象,默认为YES,Press表示特定事件的屏幕上按下按钮的存在或移动的对象*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;
B、控制同时手势识别
/*是否允许多个识别器同时识别其对应的手势,默认为NO*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
C、设置故障要求(手势的互斥)
/*建立一个动态的故障要求,YES表示第一个手势和第二个互斥时,第一个会失效,默认为NO*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
/*建立一个动态的故障要求,YES表示第一个手势和第二个互斥时,第二个会失效,默认为NO*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
3、添加和移除
/*添加目标动作*/
- (void)addTarget:(id)target action:(SEL)action;
/*删除指定的目标动作*/
- (void)removeTarget:(nullable id)target action:(nullable SEL)action;
4、获取手势的触摸位置
/*获取触发触摸的点*/
- (CGPoint)locationInView:(nullable UIView*)view;
/*获取指定触摸的点的位置*/
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
/*触摸的次数*/
@property(nonatomic, readonly) NSUInteger numberOfTouches;
/*返回触摸的次数*/
- (NSUInteger)numberOfTouches;
5、获取识别器的状态和视图
A、手势状态
/*手势的当前状态*/
@property(nonatomic,readonly) UIGestureRecognizerState state;
/*手势的状态枚举*/
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
/*默认状态,此时识别器尚未识别其手势,但可能正在评估触摸事件*/
UIGestureRecognizerStatePossible,
/*手势开始识别*/
UIGestureRecognizerStateBegan,
/*手势识别改变*/
UIGestureRecognizerStateChanged,
/*手势识别结束*/
UIGestureRecognizerStateEnded,
/*手势识别取消*/
UIGestureRecognizerStateCancelled,
/*手势识别失败*/
UIGestureRecognizerStateFailed,
/*手势识别完成,在运行循环的下一个周期发送其动作消息(或消息),并将其状态重置为UIGestureRecognizerStatePossible状态*/
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
B、手势的视图
@property(nullable, nonatomic,readonly) UIView *view;
C、手势是否有效
/*开启和禁用手势,默认为YES*/
@property(nonatomic, getter=isEnabled) BOOL enabled;
6、取消和延迟触摸
/* 默认为YES,这种情况下当手势识别器识别到touch之后,会发送touchesCancelled:withEvent:消息给视图以取消该视图对touch的响应,这个时候只有手势识别器响应touch。当设置成NO时,手势识别器识别到touch之后不会发送touchesCancelled:withEvent:消息给视图,这个时候手势识别器和视图均响应touch*/
@property(nonatomic) BOOL cancelsTouchesInView;
/*默认是NO,这种情况下当发生一个touch时,手势识别器先捕捉到到touch,然后发给视图,两者各自做出响应。如果设置为YES,手势识别器在识别的过程中(注意是识别过程),不会将touch发给视图,即视图不会有任何触摸事件。只有在识别失败之后才会将touch发给视图,这种情况下视图的响应会延迟约0.15ms*/
@property(nonatomic) BOOL delaysTouchesBegan;
/*默认为YES,这种情况下发生一个touch时,在手势识别成功后,发送给touchesCancelled:withEvent:消息给视图,手势识别失败时,会延迟大概0.15ms,期间没有接收到别的touch才会发送touchesEnded。如果设置为NO,则不会延迟,即会立即发送touchesEnded以结束当前触摸*/
@property(nonatomic) BOOL delaysTouchesEnded;
7、指定手势识别器之间的依赖关系
/*设置优先触发的手势,第一个参数是需要失效的手势,第二个是生效的手势,只有在第二个手势失效的情况下才会触发第一个手势*/
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
8、识别不同的手势
/*按压*/
@property(nonatomic, copy) NSArray *allowedPressTypes NS_AVAILABLE_IOS(9_0);
/*触摸*/
@property(nonatomic, copy) NSArray *allowedTouchTypes NS_AVAILABLE_IOS(9_0);
/*是否考虑不同类型的触摸,默认为YES*/
@property (nonatomic) BOOL requiresExclusiveTouchType NS_AVAILABLE_IOS(9_2);
网友评论