直接處理 `touchesBegan/Moved/Ended:` 已成歷史。`UIGestureRecognizer` 封裝狀態機、多點觸控、延遲識別、委派協調,是標準做法。本文介紹兩種實現層級。
| 類別 | 手勢 | 典型用途 |
|---|---|---|
| UITapGestureRecognizer | 單擊、雙擊、多點點擊 | 按鈕替代、項目選擇、縮放重置 |
| UIPinchGestureRecognizer | 雙指捏合 | 圖片縮放、地圖縮放 |
| UIRotationGestureRecognizer | 雙指旋轉 | 圖片旋轉、物體旋轉 |
| UISwipeGestureRecognizer | 快速滑動(上下左右) | 頁面切換、刪除、抽屜菜單 |
| UIPanGestureRecognizer | 拖拽/平移 | 視圖拖動、抽屜、彈簧動畫 |
| UILongPressGestureRecognizer | 長按 | 進入編輯模式、顯示菜單、預覽 |
| UIScreenEdgePanGestureRecognizer | 邊緣滑動 | 返回手勢、側邊欄呼出 |
- (void)viewDidLoad {
[super viewDidLoad];
// 目標視圖:只在這個範圍內識別滑動
UIView *detectView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 300)];
detectView.backgroundColor = [UIColor blueColor];
detectView.alpha = 0.6;
[self.view addSubview:detectView];
detectView.userInteractionEnabled = YES; // 關鍵!
// 右滑手勢
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[detectView addGestureRecognizer:swipeRight];
// 左滑手勢
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[detectView addGestureRecognizer:swipeLeft];
// 可選:上/下滑
// UISwipeGestureRecognizer *swipeUp = ... direction:UISwipeGestureRecognizerDirectionUp;
// UISwipeGestureRecognizer *swipeDown = ... direction:UISwipeGestureRecognizerDirectionDown;
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@"向左滑動 → 下一頁/刪除/下一項");
break;
case UISwipeGestureRecognizerDirectionRight:
NSLog(@"向右滑動 → 上一頁/返回/取消刪除");
break;
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"向上滑動 → 收起/刷新/展開");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"向下滑動 → 關閉/下拉刷新/收起鍵盤");
break;
default:
break;
}
}
delegate:實現 `UIGestureRecognizerDelegate` 協調多手勢(如同時識別、失敗要求)cancelsTouchesInView:預設 YES,識別成功取消傳遞 touch 給視圖。設 NO 可同時處理delaysTouchesBegan/Ended:延遲觸摸傳遞,避免閃爍numberOfTapsRequired、`numberOfTouchesRequired`:點擊次數、手指數
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// 允許 Pan 與 Swipe 同時識別
return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&
[otherGestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// 單擊需等待雙擊失敗
return [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
[otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
((UITapGestureRecognizer *)otherGestureRecognizer).numberOfTapsRequired == 2;
}
警告:使用 `GraphicsServices` 私有框架,**App Store 會拒審**,僅適用於:
// 僅供參考,不可用於上架 App
#import
- (void)simulateKeyPress:(char)keyChar {
GSEventRecord header;
GSHardwareKeyInfo keyInfo = {0};
keyInfo.keyCode = keyChar; // 簡化,實際需映射鍵碼
keyInfo.flags = 1; // key down
memset(&header, 0, sizeof(header));
header.type = kGSEventKeyDown;
header.infoSize = sizeof(GSHardwareKeyInfo);
header.timestamp = mach_absolute_time();
struct { GSEventRecord h; GSHardwareKeyInfo k; } event = {header, keyInfo};
GSSendEvent(&event, GSGetPurpleApplicationPort());
// Key Up
keyInfo.flags = 0;
header.type = kGSEventKeyUp;
header.timestamp = mach_absolute_time();
struct { GSEventRecord h; GSHardwareKeyInfo k; } eventUp = {header, keyInfo};
GSSendEvent(&eventUp, GSGetPurpleApplicationPort());
}
觸摸發生
↓
UIApplication -> UIWindow -> 根視圖控制器 -> 視圖層級遍歷 (hitTest:withEvent:)
↓
找到第一響應者 (First Responder)
ↂ
touchesBegan:withEvent: -> touchesMoved: -> touchesEnded:/touchesCancelled:
↓
若視圖未處理,沿響應者鏈向上傳遞
↓
UIWindow -> UIApplication -> AppDelegate