手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。
一、介绍
1、iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。
- UIPanGestureRecognizer(拖动)
- UIPinchGestureRecognizer(捏合)
- UIRotationGestureRecognizer(旋转)
- UITapGestureRecognizer(点按)
- UILongPressGestureRecognizer(长按)
- UISwipeGestureRecognizer(轻扫)
在六种手势识别中,只有一种手势是离散型手势,他就是 UITapGestureRecognizer。
离散型手势的特点就是:一旦识别就无法取消,而且只会调用一次手势操作事件(初始化手势时指定的回调方法)。
换句话说其他五种手势是连续型手势,而连续型手势的特点就是:会多次调用手势操作事件,而且在连续手势识别后可以取消手势。
2、手势的枚举:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
UIGestureRecognizerStateChanged, // 手势状态发生转变
UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)
UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态
UIGestureRecognizerStateRecognized= UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
3、使用手势的步骤
使用手势很简单,分为三步:
(1)创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。
(2)设置手势识别器对象实例的相关属性(可选部分)
(3)添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。
二、代码示例
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 100, 200)];
[self.view addSubview:imageView];
// 点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tick:)];
// 手指点击屏幕的次数
tap.numberOfTapsRequired = 1;
// 几个手指点击
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
// 长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(loagPress:)];
// 最少按多少秒
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
// 轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
// 轻扫的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe];
// 拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[pan requireGestureRecognizerToFail:swipe];
[self.view addGestureRecognizer:pan];
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
rotation.rotation = 1;
[self.view addGestureRecognizer:rotation];
}
- (void)tick:(UITapGestureRecognizer *)tap
{
// 6、再出始化 这个对象的 地方 挂上代理
// NextViewController *next = [[NextViewController alloc]init];
// next.delegate = self;
//
// [self presentViewController:next animated:YES completion:nil];
imageView.transform = CGAffineTransformIdentity;
// 获取点击屏幕的位置
NSLog(@"tap%f %f",[tap locationInView:self.view].x,[tap locationInView:self.view].y);
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.center = [tap locationInView:self.view];
[UIView animateWithDuration:0.5 animations:^{
imageView.alpha = 0.01;
}];
}
- (void)mmmmmm
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
}
- (void)loagPress:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPress%f %f",[longPress locationInView:self.view].x,[longPress locationInView:self.view].y);
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"swipe%f %f",[swipe locationInView:self.view].x,[swipe locationInView:self.view].y);
self.view.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView animateWithDuration:0.8 animations:^{
self.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 获取拖动的位置
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.center = [pan locationInView:self.view];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
// 捏合的变化规模
pinch.scale = 1;
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
imageView.image = [UIImage imageNamed:@"yu.jpg"];
imageView.alpha = 1;
// 使旋转手势上的视图旋转变化
imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
}