天天看點

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;      

轉載于:https://www.cnblogs.com/Zp3sss/p/9274801.html

繼續閱讀