你真的了解UIGestureRecognizer嗎?


一:首先查看一下關於UIGestureRecognizer的定義

//當前手勢狀態
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    //尚未識別是何種手勢操作(但可能已經觸發了觸摸事件),默認狀態
    UIGestureRecognizerStatePossible,   
    //手勢已經開始,此時已經被識別,但是這個過程中可能發生變化,手勢操作尚未完成
    UIGestureRecognizerStateBegan,     
    //手勢狀態發生改變
    UIGestureRecognizerStateChanged, 
    // 手勢識別操作完成(此時已經松開手指)  
    UIGestureRecognizerStateEnded, 
    //手勢被取消,恢復到默認狀態   
    UIGestureRecognizerStateCancelled, 
    //手勢識別失敗,恢復到默認狀態
    UIGestureRecognizerStateFailed,    
    //手勢識別完成,同end
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded 
};

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject

//創建一個手勢對象並添加觸發事件
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action NS_DESIGNATED_INITIALIZER; 
//給一個手勢對象添加監聽事件
- (void)addTarget:(id)target action:(SEL)action;   
//移除一個手勢的監聽事件
- (void)removeTarget:(nullable id)target action:(nullable SEL)action; 
//獲取當前手勢狀態
@property(nonatomic,readonly) UIGestureRecognizerState state;  
//委托
@property(nullable,nonatomic,weak) id <UIGestureRecognizerDelegate> delegate; 
//手勢識別是否可用
@property(nonatomic, getter=isEnabled) BOOL enabled;  
//獲取手勢觸摸的View視圖 只讀
@property(nullable, nonatomic,readonly) UIView *view;         
//是否取消觸摸控件的響應
默認為YES,這種情況下當手勢識別器識別到觸摸之后,會發送touchesCancelled給觸摸到的控件以取消控件view對touch的響應,這個時候只有手勢識別器響應touch,當設置成NO時,手勢識別器識別到觸摸之后不會發送touchesCancelled給控件,這個時候手勢識別器和控件view均響應touch。
注意:手勢識別和觸摸事件是同時存在的,只是因為touchesCancelled導致觸摸事件失效、
@property(nonatomic) BOOL cancelsTouchesInView; 
      
//是否延遲發送觸摸事件給觸摸到的控件
默認是NO,這種情況下當發生一個觸摸時,手勢識別器先捕捉到到觸摸,然后發給觸摸到的控件,兩者各自做出響應。如果設置為YES,手勢識別器在識別的過程中(注意是識別過程),不會將觸摸發給觸摸到的控件,即控件不會有任何觸摸事件。只有在識別失敗之后才會將觸摸事件發給觸摸到的控件,這種情況下控件view的響應會延遲約0.15ms。
@property(nonatomic) BOOL delaysTouchesBegan;     
  
//如果觸摸識別失敗是否立即結束本次手勢識別的觸摸事件
@property(nonatomic) BOOL delaysTouchesEnded;        

//指定一個手勢需要另一個手勢執行失敗才會執行,同時觸發多個手勢使用其中一個手勢的解決辦法
有時手勢是相關聯的,如單機和雙擊,點擊和長按,點下去瞬間可能只會識別到單擊無法識別其他,該方法可以指定某一個 手勢,即便自己已經滿足條件了,也不會立刻觸發,會等到該指定的手勢確定失敗之后才觸發
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

//獲取當前觸摸在指定視圖上的點
- (CGPoint)locationInView:(nullable UIView*)view;                               

//獲取觸摸手指數
- (NSUInteger)numberOfTouches;                                         

//多指觸摸的觸摸點相對於指定視圖的位置
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view; 

@end

UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢

知識點1:關於UIGestureRecognizer的子類如下(下面這些才是我們平常會直接運用到的類):

UITapGestureRecognizer(輕觸,點按)
UILongPressGestureRecognizer(長按)
UISwipeGestureRecognizer(輕掃手勢)
UIRotationGestureRecognizer(旋轉手勢)
UIPanGestureRecognizer(拖拽手勢)
UIPinchGestureRecognizer(捏合手勢,縮放用)

實例如下:

//拖拽手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[tagButton addGestureRecognizer:pan];


- (void)pan:(UIPanGestureRecognizer *)pan
{
             
}
//給視圖增加點擊動作

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandle:)];
[self.view addGestureRecognizer:tap];


-(void)tapHandle:(UITapGestureRecognizer*)tap
{
    
}

二:關於UIGestureRecognizerDelegate代理的內容

@protocol UIGestureRecognizerDelegate <NSObject>
@optional

//開始進行手勢識別時調用的方法,返回NO則結束識別,不再觸發手勢,用處:可以在控件指定的位置使用手勢識別
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

//是否支持多手勢觸發,返回YES,則可以多個手勢一起觸發方法,返回NO則為互斥
是否允許多個手勢識別器共同識別,一個控件的手勢識別后是否阻斷手勢識別繼續向下傳播,默認返回NO;如果為YES,響應者鏈上層對象觸發手勢識別后,如果下層對象也添加了手勢並成功識別也會繼續執行,否則上層對象識別后則不再繼續傳播
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

// 這個方法返回YES,第一個手勢和第二個互斥時,第一個會失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

//這個方法返回YES,第一個和第二個互斥時,第二個會失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

//手指觸摸屏幕后回調的方法,返回NO則不再進行手勢識別,方法觸發等
此方法在window對象在有觸摸事件發生時,調用gesture recognizer的touchesBegan:withEvent:方法之前調用,如果返回NO,則gesture recognizer不會看到此觸摸事件。(默認情況下為YES)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

@end

知識點1:

//是否同時支持多種手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
  return YES;
}

//是否允許開始點擊
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
//設置點擊的范圍
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//獲取當前的觸摸點
  CGPoint curp = [touch locationInView:self.imageView];
  if (curp.x <= self.imageView.bounds.size.width*0.5) {
      return NO;
  }else{

      return YES;
  }
}

知識點2UITapGestureRecognizer和UIButton的點擊事件沖突的解決辦法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    
if ([touch.view isKindOfClass:[UIButton class]])    { 
       return NO;    }   
       return YES;
}

三:關於UIGestureRecognizer的子類知識

1:UITapGestureRecognizer(輕觸,點按)

NS_CLASS_AVAILABLE_IOS(3_2) @interface UITapGestureRecognizer : UIGestureRecognizer

//設置能識別到手勢的最少的輕觸次數(默認為1)
@property (nonatomic) NSUInteger  numberOfTapsRequired;     
//設置能識別到手勢的最少的手指的個數(默認為1) 
@property (nonatomic) NSUInteger  numberOfTouchesRequired;  
@end

知識點1:實例代碼如下

// 創建一個手勢對象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 設置能識別到手勢的最少的輕觸次數
tap.numberOfTapsRequired = 3;
// 設置能識別到手勢的最少的手指的個數
tap.numberOfTouchesRequired = 2;
//把手勢對象添加到對應的控件中
[self.imgView addGestureRecognizer:tap];

2:UILongPressGestureRecognizer(長按手勢)

NS_CLASS_AVAILABLE_IOS(3_2) @interface UILongPressGestureRecognizer : UIGestureRecognizer
//設置能識別到手勢的最少的輕觸次數(默認為1)
@property (nonatomic) NSUInteger numberOfTapsRequired;     
//設置能識別到手勢的最少的手指的個數(默認為1) 
@property (nonatomic) NSUInteger numberOfTouchesRequired;   
//設置能識別到長按手勢的最短的長按時間,單位:秒,默認為0.5
@property (nonatomic) CFTimeInterval minimumPressDuration; 
//設置長按時允許移動的最大距離,單位:像素,默認為10像素
@property (nonatomic) CGFloat allowableMovement;           

@end

知識點1:實例代碼如下

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 設置能識別到長按手勢的最小的長按時間
longPress.minimumPressDuration = 0.5;
// "容錯的范圍"
longPress.allowableMovement  = 10;
// 把長按手勢添加到對應的控件中
[self.imgView addGestureRecognizer:longPress];

3:UISwipeGestureRecognizer(輕掃手勢)

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
    UISwipeGestureRecognizerDirectionRight = 1 << 0,  //向右滑
    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,  //向左滑
    UISwipeGestureRecognizerDirectionUp    = 1 << 2,  //向上滑
    UISwipeGestureRecognizerDirectionDown  = 1 << 3  //向下滑
};

NS_CLASS_AVAILABLE_IOS(3_2) @interface UISwipeGestureRecognizer : UIGestureRecognizer 
//最少觸摸手指個數,默認為1
@property(nonatomic) NSUInteger                        numberOfTouchesRequired; 
//設置輕掃手勢支持的方向,默認為向右滑
@property(nonatomic) UISwipeGestureRecognizerDirection direction;               

@end

知識點1:實例代碼如下

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imgView addGestureRecognizer:swipeLeft];

4:UIRotationGestureRecognizer(旋轉手勢)

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIRotationGestureRecognizer : UIGestureRecognizer
//旋轉的角度
@property (nonatomic)          CGFloat rotation;  
//旋轉速度,單位:度/秒、         
@property (nonatomic,readonly) CGFloat velocity;           

@end

知識點1:實例代碼如下

//為圖片框添加一個旋轉手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];rotation.delegate = self;
[self.imgView addGestureRecognizer:rotation];


// 旋轉手勢的監聽方法
- (void)rotateAction:(UIRotationGestureRecognizer *)recognizer {
// 在原來的基礎上, 累加多少度
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
// 每次旋轉完畢后將rotation的值, 恢復到0的位置.recognizer.rotation = 0;
}

5:UIPanGestureRecognizer(拖拽手勢)

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPanGestureRecognizer : UIGestureRecognizer 
//設置觸發拖拽最少手指數,默認為1
@property (nonatomic)          NSUInteger minimumNumberOfTouches;   
//設置觸發拖拽最多手指數,默認為 UINT_MAX 無限大
@property (nonatomic)          NSUInteger maximumNumberOfTouches;   
//獲取當前拖拽位置
- (CGPoint)translationInView:(nullable UIView *)view;                        
//設置當前拖拽位置
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
//設置拖拽速度,單位:像素/秒
- (CGPoint)velocityInView:(nullable UIView *)view;                          

@end

知識點1:實例代碼

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imgView addGestureRecognizer:pan];


// 拖拽手勢的監聽方法
- (void)panAction:(UIPanGestureRecognizer *)recognizer {
// 1. 獲取手指拖拽的時候, 平移的值
CGPoint translation = [recognizer translationInView:recognizer.view];
// 2. 讓當前控件做響應的平移
recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
// 3. 每次平移手勢識別完畢后, 讓平移的值不要累加
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}

6:UIPinchGestureRecognizer(捏合手勢,縮放用)

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPinchGestureRecognizer : UIGestureRecognizer 

//設置縮放比例
@property (nonatomic)          CGFloat scale;     
//獲取捏合速度,單位:縮放比/秒         
@property (nonatomic,readonly) CGFloat velocity;            

@end

知識點1:實例代碼

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
pinch.delegate = self;
[self.imgView addGestureRecognizer:pinch];



// 捏合手勢監聽方法
- (void)pinchAction:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1.0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM