在 iOS 裡處理使用者互動,手勢識別幾乎是繞不開的一環。本文從觸摸事件的局限講起,梳理手勢識別器的使用步驟、狀態機制與常見子類,並附上完整的實作範例。
若想監聽某個 view 上的觸摸事件,傳統做法有兩種:一是自定義一個 view,二是在其 touches 方法內部撰寫具體的處理邏輯。
然而,這種依賴 touches 方法的方式存在明顯缺陷:
正因如此,蘋果從 iOS 3.2 起推出了手勢識別功能(Gesture Recognizer),大幅降低了開發者在觸摸處理上的門檻。
UIGestureRecognizer 是一個抽象類,它定義了所有手勢的通用行為,真正處理具體手勢時需要用到它的各個子類。

使用手勢識別,大致遵循以下五個步驟:
與觸摸事件類似,手勢識別實例帶有一個 state 屬性,用於表達當前手勢所處的階段,它是枚舉類型:
不同子類各自擁有獨特的屬性與方法,以下幾個較為常用:
輕點手勢:可設定點擊次數與手指數量。
tap.numberOfTapsRequired = 2; //連續敲擊 2 次才觸發 tap.numberOfTouchesRequired = 2; //需要 2 根手指一起敲擊
旋轉手勢:可取得旋轉角度與旋轉速度。
CGFloat rotation; //旋轉角度 CGFloat velocity; //旋轉速度
輕掃手勢:一個輕掃識別物件只能對應一個方向。
UISwipeGestureRecognizerDirection //掃動方向
拖拽手勢:可取得移動距離,並在需要時清除以重新計算。
- (CGPoint)translationInView:(nullable UIView *)view; //取得移動距離 [pan setTranslation:CGPointZero inView:self.view]; //清除移動距離
有時我們希望縮放與旋轉能同時進行。做法很簡單:先為手勢識別器設置代理,再實作相應的代理方法,即可支持多手勢並存。
下面示範長按、旋轉、縮放、輕掃與拖拽等多種手勢的綁定方式:

#pragma mark - 長按手勢
- (IBAction)longPressBtnClick:(id)sender {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longClick:)];
[self.view addGestureRecognizer:longPress];
longPress.delegate = self;
}
-(void)longClick:(UILongPressGestureRecognizer *)longPress {
if(longPress.state==UIGestureRecognizerStateBegan) {
NSLog(@"開始長按了~~");
}
}
#pragma mark - 旋轉手勢
- (IBAction)rotateBtnClick {
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateClick:)];
[self.view addGestureRecognizer:rotate];
rotate.delegate = self;
}
-(void)rotateClick:(UIRotationGestureRecognizer *)rotate {
CGFloat ratation = rotate.rotation;
self.headerView.transform = CGAffineTransformRotate(self.headerView.transform, ratation);
rotate.rotation = 0; //清除累加的 rotation
}
#pragma mark - 縮放手勢
- (IBAction)pinchBtnClick {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchClick:)];
[self.view addGestureRecognizer:pinch];
pinch.delegate = self;
}
-(void)pinchClick:(UIPinchGestureRecognizer *)pinch {
CGFloat scale = pinch.scale;
self.headerView.transform = CGAffineTransformScale(self.headerView.transform, scale, scale);
pinch.scale = 1; //同樣清除
}
#pragma mark - 輕掃手勢
- (IBAction)swipeBtnClick {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
swipe2.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe2];
}
-(void)swipeClick:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction==UISwipeGestureRecognizerDirectionRight) {
[UIView animateWithDuration:0.3 animations:^{ self.headerView.transform = CGAffineTransformTranslate(self.headerView.transform, 100, 0); }completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{ self.headerView.transform = CGAffineTransformIdentity; }];
}];
} else if (swipe.direction==UISwipeGestureRecognizerDirectionLeft) {
[UIView animateWithDuration:0.3 animations:^{ self.headerView.transform = CGAffineTransformTranslate(self.headerView.transform, -100, 0); }completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{ self.headerView.transform = CGAffineTransformIdentity; }];
}];
}
}
#pragma mark - 拖拽手勢
- (IBAction)panBtnClick {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClick:)];
[self.view addGestureRecognizer:pan];
}
-(void)panClick:(UIPanGestureRecognizer *)pan {
CGPoint point = [pan translationInView:self.view];
self.headerView.transform = CGAffineTransformTranslate(self.headerView.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:self.view];
}
//手勢識別代理實現
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}