先看下效果
手势相关的介绍
ios中手势操作一般是 uigesturerecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种:
1、点击 uitapgesturerecognizer
2、平移 uipangesturerecognizer
3、缩放 uipinchgesturerecognizer
4、旋转 uirotationgesturerecognizer
5、轻扫 uiswipegesturerecognizer
我们上面这个实例中就用到了上面这5种手势,不过其中 点击与轻扫没有体现出来,只是输出了下日志而已,一会看代码
下面我们来分别介绍下这几种手势
1、uitapgesturerecognizer 点击手势
1
2
3
|
uitapgesturerecognizer* tapges = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(tapges:)]; // 点击次数,默认为1,1为单击,2为双击 tapges.numberoftapsrequired = 2; |
这个点击手势类有一个属性 numberoftapsrequired 用于设置点击数,就是点击几次才触发这个事件
2、uipangesturerecognizer 平移手势
1
2
3
4
5
6
7
8
9
|
// 平移手势 - ( void )initpanges{ uipangesturerecognizer* panges = [[uipangesturerecognizer alloc]initwithtarget:self action:@selector(panges:)]; [self.imgview addgesturerecognizer:panges]; } - ( void )panges:(uipangesturerecognizer*)ges{ // 获取平移的坐标点 cgpoint transpoint = [ges translationinview:self.imgview]; } |
平移手势本身没太多可设置的属性,在平移事件触发手,可以用 translationinview 方法获取当前平移坐标点
3、uipinchgesturerecognizer 缩放手势
1
2
3
4
5
6
7
8
9
|
// 缩放手势 - ( void )initpinges{ uipinchgesturerecognizer* pinges = [[uipinchgesturerecognizer alloc]initwithtarget:self action:@selector(pinges:)]; [self.imgview addgesturerecognizer:pinges]; } - ( void )pinges:(uipinchgesturerecognizer*)ges{ // 缩放 self.imgview.transform = cgaffinetransformscale(self.imgview.transform, ges.scale, ges.scale); } |
缩放手势在事件里面可以获取 scale 属性,表示当前缩放值
4、uirotationgesturerecognizer 旋转手势
1
2
3
4
5
6
7
8
9
|
// 旋转手势 - ( void )initrotation{ uirotationgesturerecognizer* rotationges = [[uirotationgesturerecognizer alloc]initwithtarget:self action:@selector(rotationges:)]; [self.imgview addgesturerecognizer:rotationges]; } - ( void )rotationges:(uirotationgesturerecognizer*)ges{ // 旋转图片 self.imgview.transform = cgaffinetransformrotate(self.imgview.transform, ges.rotation); } |
旋转手势在事件里面可以通过获取 rotation 属性获取当前旋转的角度
5、uiswipegesturerecognizer 轻扫手势
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// 轻扫手势 - ( void )initswipeges{ // 创建 从右向左 轻扫的手势 uiswipegesturerecognizer* swipeleftges = [[uiswipegesturerecognizer alloc]initwithtarget:self action:@selector(swipeges:)]; // 方向,默认是从左往右 // 最多只能开启一个手势,如果要开启多个就得创建多个手势 // 监听从右向左的方向 swipeleftges.direction = uiswipegesturerecognizerdirectionleft; [self.imgview addgesturerecognizer:swipeleftges]; } - ( void )swipeges:(uiswipegesturerecognizer*)ges{ // ges.direction方向值 nslog(@ "%s diection:%lu" ,__func__,(unsigned long )ges.direction); } |
轻扫手势对象需要设置 direction 属性,默认是只监听从左向右,这是一个枚举值 uiswipegesturerecognizerdirection
1
2
3
4
|
uiswipegesturerecognizerdirectionright 从左向右(默认值) uiswipegesturerecognizerdirectionleft 从右向左 uiswipegesturerecognizerdirectionup 从下向上 uiswipegesturerecognizerdirectiondown 从上向下 |
下面看一下我们上面那个效果图实现代码吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// // viewcontroller.m // 各种手势操作 // // created by xgao on 16/3/24. // copyright © 2016年 xgao. all rights reserved. // #import "viewcontroller.h" @interface viewcontroller ()<uigesturerecognizerdelegate> @property (weak, nonatomic) iboutlet uiimageview *imgview; @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; [self inittapges]; [self initpanges]; [self initpinges]; [self initrotation]; [self initswipeges]; } // 点击手势 - ( void )inittapges{ uitapgesturerecognizer* tapges = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(tapges:)]; // 点击次数,默认为1,1为单击,2为双击 tapges.numberoftapsrequired = 2; tapges.delegate = self; [self.imgview addgesturerecognizer:tapges]; } - ( void )tapges:(uitapgesturerecognizer*)ges{ nslog(@ "%s" ,__func__); } // 平移手势 - ( void )initpanges{ uipangesturerecognizer* panges = [[uipangesturerecognizer alloc]initwithtarget:self action:@selector(panges:)]; panges.delegate = self; [self.imgview addgesturerecognizer:panges]; } - ( void )panges:(uipangesturerecognizer*)ges{ // 获取平移的坐标点 cgpoint transpoint = [ges translationinview:self.imgview]; // 在之前的基础上移动图片 self.imgview.transform = cgaffinetransformtranslate(self.imgview.transform, transpoint.x, transpoint.y); // 复原,必需复原 // 每次都清空一下消除坐标叠加 [ges settranslation:cgpointzero inview:self.imgview]; nslog(@ "%s" ,__func__); } // 缩放手势 - ( void )initpinges{ uipinchgesturerecognizer* pinges = [[uipinchgesturerecognizer alloc]initwithtarget:self action:@selector(pinges:)]; pinges.delegate = self; [self.imgview addgesturerecognizer:pinges]; } - ( void )pinges:(uipinchgesturerecognizer*)ges{ // 缩放 self.imgview.transform = cgaffinetransformscale(self.imgview.transform, ges.scale, ges.scale); // 复原 // 每次都清空一下消除叠加 ges.scale = 1; } // 旋转手势 - ( void )initrotation{ uirotationgesturerecognizer* rotationges = [[uirotationgesturerecognizer alloc]initwithtarget:self action:@selector(rotationges:)]; rotationges.delegate = self; [self.imgview addgesturerecognizer:rotationges]; } - ( void )rotationges:(uirotationgesturerecognizer*)ges{ // 旋转图片 self.imgview.transform = cgaffinetransformrotate(self.imgview.transform, ges.rotation); // 复原 // 每次都清空一下消除叠加 ges.rotation = 0; nslog(@ "%s" ,__func__); } // 轻扫手势 - ( void )initswipeges{ // 创建 从右向左 轻扫的手势 uiswipegesturerecognizer* swipeleftges = [[uiswipegesturerecognizer alloc]initwithtarget:self action:@selector(swipeges:)]; // 方向,默认是从左往右 // 最多只能开启一个手势,如果要开启多个就得创建多个手势 // 监听从右向左的方向 swipeleftges.direction = uiswipegesturerecognizerdirectionleft; swipeleftges.delegate = self; [self.imgview addgesturerecognizer:swipeleftges]; // 创建 从下向上 轻扫的手势 uiswipegesturerecognizer* swipeupges = [[uiswipegesturerecognizer alloc]initwithtarget:self action:@selector(swipeges:)]; // 监听从下向上的方向 swipeupges.direction = uiswipegesturerecognizerdirectionup; swipeupges.delegate = self; [self.imgview addgesturerecognizer:swipeupges]; } - ( void )swipeges:(uiswipegesturerecognizer*)ges{ // ges.direction方向值 nslog(@ "%s diection:%lu" ,__func__,(unsigned long )ges.direction); } #pragma mark - uigesturerecognizerdelegate // 判断是否能触发手势 - ( bool )gesturerecognizershouldbegin:(uitapgesturerecognizer *)gesturerecognizer{ return yes; } // 是否允许多手势操作,不是多触摸点 - ( bool )gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer{ return yes; } @end |
这里需要注意的有两点:
1、对于 平移、缩放、旋转 这3个手势,我们如果要用它的值去处理的话,要记得复原!复原!复原!这点很重要!重要的事说3遍~~
平移手势里面我们需要设置 settranslation:cgpointzero 来复原它的坐标值,不然下一次事件触发这个坐标值会叠加
缩放手势里面设置 ges.scale = 1 来复原它的缩放值
旋转手势里面设置 ges.rotation = 0 来复原它的角度值
2、假如我们需要多手势一起用的时候就需要设置下delegate 里面的一个返回参数的方法了
1
2
3
4
|
// 是否允许多手势操作,不是多触摸点 - ( bool )gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer{ return yes; } |
以上所述是小编给大家介绍的ios中各种手势操作实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/xgao/archive/2017/03/27/6626446.html