UIGestureRecognizer手勢識別器

通過touches方法監聽view觸摸事件,有很明顯的幾個缺點
必須得自定義view
由於是在view內部的touches方法中監聽觸摸事件,因此默認情況下,無法讓其他外界對象監聽view的觸摸事件
不容易區分用戶的具體手勢行爲
iOS3.2後,蘋果推出了手勢識別功能(GestureRecognizer,在觸摸事件處理方面,大大簡化了開發者的開發難度

UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行爲,使用它的子類才能處理具體的手勢
UITapGestureRecognizer(敲擊)
UIPinchGestureRecognizer(捏合,用於縮放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(輕掃)
UIRotationGestureRecognizer(旋轉)
UILongPressGestureRecognizer(長按)


每一個手勢識別器的用法都差不多,比如UITapGestureRecognizer的使用步驟如下
1、創建手勢識別器對象

UITapGestureRecognizer *tap= [[UITapGestureRecognizeralloc] init];

2、設置手勢識別器對象的具體屬性

// 連續敲擊2

tap.numberOfTapsRequired = 2;

// 需要2根手指一起敲擊

tap.numberOfTouchesRequired = 2;

3、添加手勢識別器到對應的view

[self.iconViewaddGestureRecognizer:tap];

4、監聽手勢的觸發

[tap addTarget:selfaction:@selector(tapIconView:)];


手勢識別的狀態

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

    //沒有觸摸事件發生,所有手勢識別的默認狀態

   UIGestureRecognizerStatePossible

    //一個手勢已經開始但尚未改變或者完成時

   UIGestureRecognizerStateBegan,

    // 手勢狀態改變

   UIGestureRecognizerStateChanged,

    // 手勢完成

   UIGestureRecognizerStateEnded,

    // 手勢取消,恢復至Possible狀態

    UIGestureRecognizerStateCancelled,

   //手勢失敗,恢復至Possible狀態

   UIGestureRecognizerStateFailed,

    // 識別到手勢識別

   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

};


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
敲擊手勢:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、創建一個敲擊手勢
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
    
    // 2、設置手勢對象的屬性
    // 點擊次數
    tapGesture.numberOfTapsRequired = 1;
    
    // 手指的數量
//    tapGesture.numberOfTouchesRequired = 2;
    
    // 設置代理
    tapGesture.delegate = self;
    
    // 3、把手勢添加到view上
    [self.view addGestureRecognizer:tapGesture];
    
    // 4、設置手勢的監聽方法
    [tapGesture addTarget:self action:@selector(tapView:)];

}

- (void)tapView:(UITapGestureRecognizer *)tapGest{
    NSLog(@"敲擊手勢的監聽方法");
}

長按手勢:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、創建一個 “長按手勢” 對象
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] init];
    
    // 2、設置屬性
    // 長按最少持續多長時間
    longGesture.minimumPressDuration = 2;
    // 長按時,距離 “觸摸點” 可移動的距離
    longGesture.allowableMovement = 30;
    
    // 3、把手勢添加到view上
    [self.imageView addGestureRecognizer:longGesture];
    
    // 4、設置手勢的監聽方法
    [longGesture addTarget:self action:@selector(longPressView:)];

}

- (void)longPressView:(UILongPressGestureRecognizer *)longPressGesture{
    // 怎麼判斷長按開始和結束
    // 通過手勢的狀態來判斷
    if (longPressGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"長按手勢開始");
    }else{
        NSLog(@"長按手勢結束");
    }
}

輕掃手勢:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、創建一個 “輕掃手勢” 對象
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
    
    // 2、設置屬性
    //UISwipeGestureRecognizerDirectionRight 向右輕掃
    //UISwipeGestureRecognizerDirectionLeft    向左輕掃
    //UISwipeGestureRecognizerDirectionUp    向上
    //UISwipeGestureRecognizerDirectionDown 向下
    
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    
    // 3、把手勢添加到view上
    [self.imageView addGestureRecognizer:swipeGesture];
}

- (void)swipeView:(UIGestureRecognizer *)swipeGest{
    // 怎麼判斷 "長按" 開始和結束
    NSLog(@"%s 手勢狀態 %ld",__func__, swipeGest.state);
}

捏合手勢:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、創建一個 “捏合手勢” 對象
    UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinView:)];
    
    // 2、把手勢添加到 view上
    [self.imageView addGestureRecognizer:pinchGest];
    
}

- (void)pinView:(UIPinchGestureRecognizer *)pinGest{
    
    // 縮放的比例是一個 “累加” 的過程
#warning 放大圖片後,再次縮放的時候,馬上回到原先的大小
//    self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
    
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
    
    // 讓比例還原 ,不累加
    // 解決辦法
    pinGest.scale = 1;
}

旋轉手勢:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1、添加一個 “旋轉手勢” 對像
    UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
    
    // 2、把手勢添加到 view上
    [self.imageView addGestureRecognizer:rotationGest];
}

- (void)rotationView:(UIRotationGestureRecognizer *)rotationGesture{
    
    // 旋轉角度
    // 旋轉角度也是一個累加過程
    // 設置圖片的旋轉
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
    
    // 清除 “旋轉角度” 的累加
    rotationGesture.rotation = 0;
}

旋轉、捏合手勢:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 默認情況下,控件只能監聽到一種手勢
    // 如果要監聽到多個手勢,設置一個代理的方法,告知它允許 “多個手勢” 並存
    
    // 給圖片添加一個 “旋轉手勢” 對象
    // 1、創建一個  “旋轉手勢” 對象
    UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
    
    // 設置代理
    rotationGest.delegate = self;
    
    // 2、把手勢添加到 view上
    [self.view addGestureRecognizer:rotationGest];
    
    // 3、給圖片添加 “捏合手勢”
    UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [self.imageView addGestureRecognizer:pinchGest];
    
    
}

- (void)rotationView:(UIRotationGestureRecognizer *)rotationGesture{
    
    // 旋轉角度
    // 旋轉角度也是一個累加過程
    // 設置圖片的旋轉
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGesture.rotation);
    
    // 清除 “旋轉角度” 的累加
    rotationGesture.rotation = 0;
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinGest{
    
    // 縮放的比例是一個 “累加” 的過程
#warning 放大圖片後,再次縮放的時候,馬上回到原先的大小
//    self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
    
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinGest.scale, pinGest.scale);
    
    // 讓比例還原 ,不累加
    // 解決辦法
    pinGest.scale = 1;
}


// simultaneous  同時發生
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
}

拖拽手勢:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 給圖片添加 “拖拽手勢”
    // 1、創建一個 “拖拽手勢” 對象
    UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    // 2、添加到view
    [self.imageView addGestureRecognizer:panGest];
}

- (void)panView:(UIPanGestureRecognizer *)panGest{
    
    // panGest.view 觸摸的view
    // 拖拽的距離 (距離是一個累加的過程)
    CGPoint trans = [panGest translationInView:panGest.view];
    
    // 設置圖片移動
    CGPoint center = self.imageView.center;
    center.x += trans.x;
    center.y += trans.y;
    self.imageView.center = center;
    
    // 清除累加的距離
    [panGest setTranslation:CGPointZero inView:panGest.view];
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
storyboard創建手勢:
1、選取對應的手勢

2、拖到要添加手勢的控件上

3、連線

其他手勢方法同上
發佈了51 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章