1. UIGestureRecognizer
- 父类是NSObject
- 利用手势识别器---UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
- UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势,详见其子类介绍
- 一个手势只能支持一个方向
- 默认只支持一个手势,若要实现多个手势,需要设置手势代理,遵守<UIGestureRecognizerDelegate>,并实现
shouldRecognizeSimultaneouslyWithGestureRecognizer
,返回YES。
2. 手势识别的状态
手势状态 |
含义 |
UIGestureRecognizerStatePossible |
没有触摸事件发生,所有手势识别的默认状态 |
UIGestureRecognizerStateBegan |
一个手势已经开始但尚未改变或者完成时 |
UIGestureRecognizerStateChanged |
手势状态改变 |
UIGestureRecognizerStateEnded |
手势完成 |
UIGestureRecognizerStateCancelled |
手势取消,恢复至Possible状态 |
UIGestureRecognizerStateFailed |
手势失败,恢复至Possible状态 |
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded |
识别到手势识别 |
3. UIGestureRecognizer属性
// 手势状态
@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;
@property(nonatomic) BOOL delaysTouchesBegan;
@property(nonatomic) BOOL delaysTouchesEnded;
@property(nonatomic, copy) NSArray<NSNumber *> *allowedTouchTypes
@property(nonatomic, copy) NSArray<NSNumber *> *allowedPressTypes
4. UIGestureRecognizer方法
// 初始化手势,直接用initWithTarget就好,另外两个忽略
// -(void)handleGesture;
// -(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer;
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action
// 增加监听和移除监听
- (void)addTarget:(id)target action:(SEL)action;
- (void)removeTarget:(nullable id)target action:(nullable SEL)action; //target为nil,则移除所有相同方法名的方法
// 获取touch的位置
- (CGPoint)locationInView:(nullable UIView*)view;
// 获取locations中的touches的数量
- (NSUInteger)numberOfTouches;
// 获取特定角标的touch的位置
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
@end
5. UIGestureRecognizer的代理--UIGestureRecognizerDelegate
// 是否允许触发当前手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
// Simultaneously:同时 sɪml'teɪnɪəslɪ
// 是否允许同时支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
// 是否接收触摸手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
// 是否接收按压手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
@end
6. UIGestureRecognizer的子类
6.2. 捏合手势——用于缩放