iOS开发-手势
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
:手势识别器识别到手势失败。