UI-手势识别

基类UIGestureRecogizer

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    UIGestureRecognizerStatePossible,   // 手势识别默认状态,可能为发生手势识别,或者识别还没有完成
    
    UIGestureRecognizerStateBegan,      // 手势识别完成,手势处理方法开始
    UIGestureRecognizerStateChanged,    // 手势发生改变(手指移动)。
    UIGestureRecognizerStateEnded,      // 手势处理完成,系统将还原手势状态UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // 手势处理取消,系统将还原手势状态 UIGestureRecognizerStatePossible
    
    UIGestureRecognizerStateFailed,     //手势识别器识别失败,系统还原手势状态 UIGestureRecognizerStatePossible
    

};

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject

// 手势识别器类的定义方法,并设置手势处理代理对象和该对象的方法
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action 

// 添加手势处理的代理对象和方法
- (void)addTarget:(id)target action:(SEL)action;  
// 移除手势中某个代理对象的处理方法,target传nil,action传某个方法名,表示移除所有代理对象的该方法,就像iOS通知注册和移除一样
- (void)removeTarget:(nullable id)target action:(nullable SEL)action;

// 手势识别器当前状态
@property(nonatomic,readonly) UIGestureRecognizerState state; 

// 手势识别器代理方法,用来区分不同手势之间的识别
@property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate;

// 是否接收手势,默认为YES;NO表示该手势识别器将不再接收任何手势
@property(nonatomic, getter=isEnabled) BOOL enabled;  

@property(nullable, nonatomic,readonly) UIView *view;   // 监听的UIView(addGesture:方法添加),会接收触发了hit-test方法的View(该手势监听的View)和它的subView。

//取消控件的触摸方法调用,默认为YES,如果为NO,表示手势处理方法和触摸方法一起调用。否则将发送touchCancelled方法将被调用,接触触摸方法调用。
@property(nonatomic) BOOL cancelsTouchesInView;       

// 设为YES后,只有手势识别完后,才将触摸事件发送给目标控件,触发触摸方法
@property(nonatomic) BOOL delaysTouchesBegan;        

// 只有手势识别失败后,才将触摸事件发送给目标控件,触发触摸方法,否则不会传递给控件
@property(nonatomic) BOOL delaysTouchesEnded;         /


@property(nonatomic, copy) NSArray<NSNumber *> *allowedTouchTypes 
@property(nonatomic, copy) NSArray<NSNumber *> *allowedPressTypes 

// Indicates whether the gesture recognizer will consider touches of different touch types simultaneously.
// If NO, it receives all touches that match its allowedTouchTypes.
// If YES, once it receives a touch of a certain type, it will ignore new touches of other types, until it is reset to UIGestureRecognizerStatePossible.
@property (nonatomic) BOOL requiresExclusiveTouchType NS_AVAILABLE_IOS(9_2); // defaults to YES

// 当otherGestureRecognizer识别失败后,当前手势才能识别该手势。相反识别成功后,当前手势识别失败
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

// 参照当前View的左上角为原点,计算手指触摸点到该view左上角的位置。
- (CGPoint)locationInView:(nullable UIView*)view;                 

// 获取触摸手指个数
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) NSUInteger numberOfTouches;   // number of touches involved for which locations can be queried
#else
- (NSUInteger)numberOfTouches;  // number of touches involved for which locations can be queried
#endif

// 获取某个手指的坐标,触摸的手指个数发生变化时,很容易造成数组越界
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view; // the location of a particular touch

// 识别器名称,用于调试
@property (nullable, nonatomic, copy) NSString *name 

@end


@protocol UIGestureRecognizerDelegate <NSObject>
@optional

// 手势识别完成,当返回YES时手势识别状态会从UIGestureRecognizerStatePossible转成UIGestureRecognizerStateBegan,返回NO时直接变成UIGestureRecognizerStateFailed。
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

// 设置两个手势之间冲突问题,是否允许同时处理两个有冲突的手势操作。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

// called once per attempt to recognize, so failure requirements can be determined lazily and may be set up between recognizers across view hierarchies
// return YES to set up a dynamic failure requirement between gestureRecognizer and otherGestureRecognizer
//
// note: returning YES is guaranteed to set up the failure requirement. returning NO does not guarantee that there will not be a failure requirement as the other gesture's counterpart delegate or subclass methods may return YES

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

// 在touchBegin之前调用接收该触摸事件,如果返回NO,表示不处理某个触摸事件,如果直接返回NO,表示不处理任何触摸事件。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

// called before pressesBegan:withEvent: is called on the gesture recognizer for a new press. return NO to prevent the gesture recognizer from seeing this press
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;

猜你喜欢

转载自www.cnblogs.com/Zp3sss/p/9274801.html