UIGestureRecognizer
用於檢測和處理手勢的抽象基類。提供了檢測使用者手勢的基本功能,如點按、滑動、捏合、旋轉等。透過使用 UIGestureRecognizer
子類,可以為檢視新增手勢識別功能,增強使用者互動體驗。
常見的 UIGestureRecognizer 子類
一些常見的手勢識別器子類:
- UITapGestureRecognizer:檢測點按手勢。
- UIPinchGestureRecognizer:檢測捏合(縮放)手勢。
- UIRotationGestureRecognizer:檢測旋轉手勢。
- UISwipeGestureRecognizer:檢測滑動手勢。
- UIPanGestureRecognizer:檢測平移(拖動)手勢。
- UILongPressGestureRecognizer:檢測長按手勢。
使用 UIGestureRecognizer
新增手勢識別器
- 建立手勢識別器
- 配置手勢識別器
- 將手勢識別器新增到檢視
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 300, 400)];
gestureView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:gestureView];
// 新增點按手勢識別器
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[gestureView addGestureRecognizer:tapGesture];
// 新增捏合手勢識別器
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
[gestureView addGestureRecognizer:pinchGesture];
// 新增旋轉手勢識別器
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
[gestureView addGestureRecognizer:rotationGesture];
// 新增滑動手勢識別器
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; // 設定滑動方向
[gestureView addGestureRecognizer:swipeGesture];
// 新增平移手勢識別器
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[gestureView addGestureRecognizer:panGesture];
// 新增長按手勢識別器
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[gestureView addGestureRecognizer:longPressGesture];
}
// 點按手勢處理方法
- (void)handleTap:(UITapGestureRecognizer *)gesture {
NSLog(@"Tap detected");
}
// 捏合手勢處理方法
- (void)handlePinch:(UIPinchGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateChanged) {
gesture.view.transform = CGAffineTransformScale(gesture.view.transform, gesture.scale, gesture.scale);
gesture.scale = 1.0;
}
}
// 旋轉手勢處理方法
- (void)handleRotation:(UIRotationGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateChanged) {
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation);
gesture.rotation = 0.0;
}
}
// 滑動手勢處理方法
- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture {
NSLog(@"Swipe detected");
}
// 平移手勢處理方法
- (void)handlePan:(UIPanGestureRecognizer *)gesture {
CGPoint translation = [gesture translationInView:gesture.view];
gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);
[gesture setTranslation:CGPointZero inView:gesture.view];
}
// 長按手勢處理方法
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected");
}
}
@end
手勢識別器的屬性和方法
-
屬性
enabled
:一個布林值,指示手勢識別器是否啟用。state
:手勢識別器的當前狀態(例如UIGestureRecognizerStatePossible
、UIGestureRecognizerStateBegan
等)。view
:手勢識別器附加的檢視。cancelsTouchesInView
:一個布林值,指示手勢識別器是否阻止觸控事件傳遞到檢視。
-
方法
- (void)addTarget:(id)target action:(SEL)action;
:新增目標和動作。- (void)removeTarget:(id)target action:(SEL)action;
:移除目標和動作。- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
:要求另一個手勢識別器失敗,才能識別當前手勢。
手勢識別器的狀態
手勢識別器的狀態(UIGestureRecognizerState
)有以下幾種:
UIGestureRecognizerStatePossible
:手勢識別器沒有識別到手勢,但可能會在未來識別到。UIGestureRecognizerStateBegan
:手勢識別器識別到手勢開始。UIGestureRecognizerStateChanged
:手勢識別器識別到手勢發生變化。UIGestureRecognizerStateEnded
:手勢識別器識別到手勢結束。UIGestureRecognizerStateCancelled
:手勢識別器識別到手勢被取消。UIGestureRecognizerStateFailed
:手勢識別器識別到手勢失敗。