手势,手势响应GestureRecognizer
关于手势响应IOS中封装了一个类,能响应一般手势UIGestureRecognizer
UIGestureRecognizerState的定义如下:
typedef enum {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;
Possible: 识别器在未识别出它的手势,但可能会接收到触摸时处于这个状态。这是默认状态。
Began: 识别器接收到触摸并识别出是它的手势时处于这个状态。响应方法将在下个循环步骤中被调用。
Changed:the recognizer has received touches recognized as a change to the gesture. (不懂怎么翻译,理解上就是识别器识别出一个变化为它的手势的触摸),响应方法将在下个循环步骤中被调用。
Ended:识别器在识别到作为当前手势结束信号的触摸时处于这个状态。响应方法将在下个循环步骤中被调用 并且 识别器将重置为possible状态。
Cancelled:识别器处于取消状态.响应方法将在下个循环步骤中被调用 并且 识别器将重置为possible状态。
Failed: 识别器接收到不能识别为它的手势的一系列触摸。响应方法不会被调用 并且 识别器将重置为possible状态。
Recognized: 识别器已识别到它的手势。响应方法将在下个循环步骤中被调用 并且 识别器将重置为possible状态。
下面说一下这个UIGestureRecognizer 类
这个类有几个子类,这几个子类分别表示几种不同的基本手势
1、UITapGestureRecognizer //点击手势识别器,可以是点击一次,或多次都能识别
2、UIPinchGestureRecognizer //捏合手势识别器,用于视图的放大缩小
3、UIRotationGestureRecognizer //旋转手势识别器
4、UISwipeGestureRecognizer //滑动手势识别器,向上、下、左、右滑动
5、UIPanGestureRecognizer //拖动手势识别器
6、UILongPressGestureRecognizer //长按手势识别器,常见的有长按跳出一个界面用以编辑
手势状态枚举:
1 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
2 UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
3 UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
4 UIGestureRecognizerStateChanged, // 手势状态发生转变
5 UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)
6 UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
7 UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态
8 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded
9 };
添加点击手势:
UITapGestureRecognizer *click1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click1:)];
//设置这个点击手势识别器所能响应的点击次数,单击所以设成1,如果想双击,就设置为2,以此类推
click1.numberOfTapsRequired = 1;
//添加手势到视图上
[self.view addGestureRecognizer:click1];
//当有单击手势click1和双击手势click2的时候,需要加下面的代码用来判断,优先判断谁来响应方法
[click1 requireGestureRecognizerToFail:click2];
然后实现方法click1:就可以了
-(void)click1:(UITapGestureRecognizer *)sender{
}
添加滑动手势:
UISwipeGestureRecognizer *huaDong = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huaDong:)];
//设置能识别滑动手势的方向,注意一个手势识别器只能识别一个方向上的滑动
huaDong.direction = UISwipeGestureRecognizerDirectionLeft;
huaDong.direction = UISwipeGestureRecognizerDirectionRight;
huaDong.direction = UISwipeGestureRecognizerDirectionUp;
huaDong.direction = UISwipeGestureRecognizerDirectionDown;
//添加手势到视图上
[self.view addGestureRecognizer:huaDong];
然后实现huaDong:方法就好了
-(void)huaDong:(UISwipeGestureRecognizer *)sender{
}
添加长按手势:
UILongPressGestureRecognizer *longClick = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longClick:)];
//设置长按多少秒后才识别这个手势
longClick.minimumPressDuration = 2;
//添加手势到视图上
[self.view addGestureRecognizer:longClick];
然后实现longClick:方法就好了
-(void)longClick:(UILongPressGestureRecognizer *)sender{
}
添加移动手势:
UIPanGestureRecognizer *move = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
//添加手势到视图上
[self.view addGestureRecognizer:move];
可以根据平移时的位置的移动来拖动视图,获取位置,用这个方法可以获取到当前移动到得位置
CGPoint point = [move locationInView:self.view];
添加捏合手势:
捏合有两个属性
@property (nonatomic,readonly) CGFloat velocity;// 捏合的速度
@property (nonatomic) CGFloat scale;// 比例(经常用到放缩比例) 这个属性默认值是1
//添加捏合手势
UIPinchGestureRecognizer *nieHe = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(nieHe:)];
[self.view addGestureRecognizer:nieHe];
添加旋转手势:
旋转也由两个属性
@property (nonatomic) CGFloat rotation; //旋转角度
@property (nonatomic,readonly) CGFloat velocity; //旋转速度
这个主要通过获取旋转角度来完成一些列操作
//添加旋转手势识别器
UIRotationGestureRecognizer *xuanZhuan = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(xuanZhuan:)];
[self.view addGestureRecognizer:xuanZhuan];
下面是一个完整的DEMO,直接拷贝到ViewController.m文件可以直接测试:
#import "ViewController.h"
@interface ViewController ()
// 比例(经常用到放缩比例) 这个属性默认值是1
@property (nonatomic) CGFloat scale;
// 捏合的速度/旋转速度(这个不需要设置)
@property (nonatomic,readonly) CGFloat velocity;
// 旋转角度
@property (nonatomic) CGFloat rotation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self nieHe];
}
#pragma mark 旋转手势
-(void)xuanZhuan{
// 添加一个旋转手势识别器
UIRotationGestureRecognizer *xuanZhuan = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(xuanZhuan:)];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 50, 300, 400)];
view.backgroundColor = [UIColor redColor];
[view addGestureRecognizer:xuanZhuan];
[self.view addSubview:view];
}
-(void)xuanZhuan:(UIRotationGestureRecognizer *)sender{
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
sender.rotation = 0;
NSLog(@"旋转了!!!");
}
#pragma mark 捏合手势
-(void)nieHe{
// 添加一个捏合手势识别器
UIPinchGestureRecognizer *nieHe = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(nieHe:)];
// 将手势添加到视图上面
[self.view addGestureRecognizer:nieHe];
}
-(void)nieHe:(UIPinchGestureRecognizer *)sender{
// 设置视图如何变化
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
// 最好写上这句话,测试的时候,你会发现视图找不到了。😢
sender.scale = 1;
NSLog(@"捏合啦!!");
}
#pragma mark 移动手势
-(void)move{
// 添加一个移动手势识别器
UIPanGestureRecognizer *move = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
// 添加一个view用来方便的调试
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
// 设置view的背景颜色为红色
view.backgroundColor = [UIColor redColor];
// 给view添加移动手势
[view addGestureRecognizer:move];
// 将view添加到视图上面
[self.view addSubview:view];
}
-(void)move:(UIPanGestureRecognizer *)sender{
// 用这个方法可以获取到当前手势移动到得位置
CGPoint point = [sender locationInView:self.view];
// 可以根据平移时的位置的移动来拖动视图(改变视图的Center)
sender.view.center = CGPointMake(point.x,point.y);
NSLog(@"移动到了(%f,%f)这个点",point.x,point.y);
}
#pragma mark 长按手势
-(void)changClick{
// 添加一个长按手势识别器
UILongPressGestureRecognizer *longClick = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longClick:)];
// 设置长按多少秒后才识别这个手势
longClick.minimumPressDuration = 2;
// 添加手势到视图上
[self.view addGestureRecognizer:longClick];
}
-(void)longClick:(UILongPressGestureRecognizer *)sender{
NSLog(@"长按了!!!");
}
#pragma mark 左侧滑动手势
-(void)huaDongShouShi{
// 添加一个滑动手势识别器
UISwipeGestureRecognizer *huaDongLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huaDongLeft:)];
// 添加一个滑动手势识别器
UISwipeGestureRecognizer *huaDongRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huaDongRight:)];
// 添加一个滑动手势识别器
UISwipeGestureRecognizer *huaDongUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huaDongUp:)];
// 添加一个滑动手势识别器
UISwipeGestureRecognizer *huaDongDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(huaDongDown:)];
// 设置能识别滑动手势的方向,注意一个手势识别器只能识别一个方向上的滑动,以左侧滑动为例子(如果想多方向监听滑动,可以多添加几个手势识别器)
// 左侧滑动
huaDongLeft.direction = UISwipeGestureRecognizerDirectionLeft;
// 右侧滑动
huaDongRight.direction = UISwipeGestureRecognizerDirectionRight;
// 向上滑动
huaDongUp.direction = UISwipeGestureRecognizerDirectionUp;
// 向下滑动
huaDongDown.direction = UISwipeGestureRecognizerDirectionDown;
// 添加手势到视图上
[self.view addGestureRecognizer:huaDongLeft];
[self.view addGestureRecognizer:huaDongRight];
[self.view addGestureRecognizer:huaDongUp];
[self.view addGestureRecognizer:huaDongDown];
}
-(void)huaDongLeft:(UISwipeGestureRecognizer *)sender{
NSLog(@"左滑了一下");
}
-(void)huaDongRight:(UISwipeGestureRecognizer *)sender{
NSLog(@"右滑了一下");
}
-(void)huaDongUp:(UISwipeGestureRecognizer *)sender{
NSLog(@"上滑了一下");
}
-(void)huaDongDown:(UISwipeGestureRecognizer *)sender{
NSLog(@"下滑了一下");
}
#pragma mark 点击手势
-(void)dianJiShouShi{
// 添加一个单击手势识别器
UITapGestureRecognizer *click1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click1:)];
// 其中1代表点击1次相应方法
click1.numberOfTapsRequired = 1;
// 添加一个双击手势识别器
UITapGestureRecognizer *click2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click2:)];
// 其中2代表点击2次相应方法
click2.numberOfTapsRequired = 2;
// 将手势添加到视图上
[self.view addGestureRecognizer:click1];
[self.view addGestureRecognizer:click2];
// 这句话是关键,用来设置优先考虑响应哪个手势
[click1 requireGestureRecognizerToFail:click2];
}
-(void)click1:(UITapGestureRecognizer *)sender{
NSLog(@"单击了一下");
}
-(void)click2:(UITapGestureRecognizer *)sender{
NSLog(@"双击了一下");
}