iOS 共提供六種標準手勢辨識器:
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tap.numberOfTapsRequired = 1; // 點擊次數
tap.numberOfTouchesRequired = 1; // 手指數量
[self.view addGestureRecognizer:tap];
- (void)tapAction:(UITapGestureRecognizer *)sender {
// 點擊次數與手指數可從 sender 讀取
}UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 0.5; // 最少按壓秒數
[self.view addGestureRecognizer:longPress];
- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
// 開始長按
}
}長按的常用狀態:
UIGestureRecognizerStateBegan 開始 UIGestureRecognizerStateChanged 進行中(長按會持續回呼,適合拖放) UIGestureRecognizerStateEnded 結束 UIGestureRecognizerStateCancelled 取消 UIGestureRecognizerStateFailed 失敗
每個辨識器實例只對應一個方向,要同時支援左右需各建一個。
UISwipeGestureRecognizer *swipeRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
- (void)swipeAction:(UISwipeGestureRecognizer *)sender {
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
// 向左
} else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
// 向右
}
}UIPinchGestureRecognizer *pinch =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
// scale 為相對起點的縮放比例
_imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
}
if (sender.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformIdentity; // 復原形變
}];
}
}UIPanGestureRecognizer *pan =
[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
- (void)panAction:(UIPanGestureRecognizer *)sender {
// 回傳相對於指定 view 的位移量
CGPoint move = [sender translationInView:self.view];
// 若要取得速度,可用 velocityInView:
}UIRotationGestureRecognizer *rotation =
[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
_imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
}
if (sender.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:1.0 animations:^{
_imageView.transform = CGAffineTransformIdentity;
}];
}
}實務上兩個關鍵補充:第一,捏合與旋轉的 scale、rotation 都是相對手勢起點的累積值,若要在同一個 view 上連續多次縮放而不跳動,應該在每次手勢結束後把值歸零,或改用 transform 相乘而非直接覆寫。第二,當多個手勢會互相衝突(例如單擊與雙擊),可用 [singleTap requireGestureRecognizerToFail:doubleTap] 指定優先順序。