Apple 為各類手勢提供了對應的識別器類別。本文用四個最常見的識別器做示例,每個手勢都配一個彈窗回饋,方便直觀確認效果。搖動(shake)不在本文範圍內。
先記住一個通用套路,四種手勢都是照著它走的:創建識別器實例 → 初始化並綁定 action → 配置屬性 → 添加到視圖。
如果想獲取手勢發生位置的坐標,可以這樣寫:
CGPoint location = [recognizer locationInView:self.view]; // location.x / location.y
回調方法:
- (void)foundTap:(UITapGestureRecognizer *)recognizer {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:@"tapped" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
在 viewDidLoad 中註冊:
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1; // 點擊次數
tapRecognizer.numberOfTouchesRequired = 1; // 需要幾根手指
[self.view addGestureRecognizer:tapRecognizer];
回調方法:
- (void)foundSwipe:(UISwipeGestureRecognizer *)recognizer {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:@"swiped" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
註冊時需要指定方向,四個方向可以按需組合:
UISwipeGestureRecognizer *swipeRecognizer =
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(foundSwipe:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight
| UISwipeGestureRecognizerDirectionLeft;
swipeRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:swipeRecognizer];
可選方向為 Right / Left / Up / Down。注意與 Tap 不同,一個 Swipe 識別器只能觸發一次,因此如需同時響應多個方向,通常會為每個方向各建一個實例。
用於實現放大縮小。回調方法:
- (void)foundPinch:(UIPinchGestureRecognizer *)recognizer {
double scale = recognizer.scale; // 縮放比例
// 可結合 OpenGL ES 的 glScalef 實現實際縮放
NSString *message = [NSString stringWithFormat:
@"縮放:Scale:%1.2f, Velocity:%1.2f",
recognizer.scale, recognizer.velocity];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:message delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
註冊:
UIPinchGestureRecognizer *pinchRecognizer =
[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(foundPinch)];
[self.view addGestureRecognizer:pinchRecognizer];
scale 是相對於手勢起點的倍數,velocity 則是變化速度,做跟手動畫時常用它來判斷方向與力度。
這裡有個容易踩的坑:Cocoa 系框架一律使用弧度作為角度單位,換算關係是:
角度 = 弧度 * 180 / M_PI
回調方法:
- (void)foundRotation:(UIRotationGestureRecognizer *)recognizer {
double rotation = recognizer.rotation; // 弧度
// 可結合 glRotatef 實現實際旋轉
NSString *message = [NSString stringWithFormat:
@"旋轉,Radians:%1.2f, Velocity:%1.2f",
recognizer.rotation, recognizer.velocity];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
message:message delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
註冊:
UIRotationGestureRecognizer *rotationRecognizer =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:@selector(foundRotation:)];
[self.view addGestureRecognizer:rotationRecognizer];
多個識別器共存時,難免會互相搶事件。這時應該實現 UIGestureRecognizerDelegate 的 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 返回 YES,允許並發識別。
如果手勢需要與 UIScrollView 共存(例如全屏滑動翻頁),可以通過 gestureRecognizer:shouldReceiveTouch: 過濾掉落在特定子視圖上的觸摸。
本文示例中的 UIAlertView 在 iOS 7 之後已建議改用 UIAlertController;手勢演示時更推薦直接改動一個 label 或 imageView 的屬性,回饋更直觀也不打斷操作。