感觉有必要把ios开发中的手势识别做一个小小的总结。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,也是目标 动作回调,话不多说,切入今天的正题。
总共有六种手势识别:轻击手势(tapgesturerecognizer),轻扫手势 (swipegesturerecognizer), 长按手势(longpressgesturerecognizer), 拖动手势(pangesturerecognizer), 捏合手势(pinchgesturerecognizer),旋转手势(rotationgesturerecognizer);
其实这些手势用touche事件完全可以实现,苹果就是把常用的触摸事件封装成手势,来提供给用户。读者完全可以用touchesmoved来写拖动手势等
一,用storyboard给控件添加手势识别
1.用storyboard添加手势识别,和添加一个button的步骤一样,首先我们得找到相应的手势,把手势识别的控件拖到我们要添加手势的控件中,截图如下:
2.给我们拖出的手势添加回调事件,和给button回调事件没啥区别的,在回调方法中添加要实现的业务逻辑即可,截图如下:
二,纯代码添加手势识别
用storyboard可以大大简化我们的操作,不过纯代码的方式还是要会的,就像要dreamwear编辑网页一样(当然啦,storyboard的拖拽功能要比dreamwear的拖拽强大的多),用纯代码敲出来的更为灵活,更加便 于维护。不过用storyboard可以减少我们的工作量,这两个要配合着使用才能大大的提高我们的开发效率。个人感觉用storyboard把框架搭起 来(controller间的关系),一下小的东西还是用纯代码敲出来更好一些。下面就给出如何给我们的控件用纯代码的方式来添加手势识别。
1.轻击手势(tapgesturerecognizer)的添加
初始化代码tapgesturerecongnizer的代码如下:
1
2
3
4
5
6
|
//新建tap手势
uitapgesturerecognizer *tapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapgesture:)];
//设置点击次数和点击手指数
tapgesture.numberoftapsrequired = 1; //点击次数
tapgesture.numberoftouchesrequired = 1; //点击手指数
[self.view addgesturerecognizer:tapgesture];
|
在回调方法中添加相应的业务逻辑:
1
2
3
4
5
|
//轻击手势触发方法
-( void )tapgesture:(id)sender
{
//轻击后要做的事情
}
|
2.长按手势(longpressgesturerecognizer)
初始化代码:
1
2
3
4
5
|
//添加长摁手势
uilongpressgesturerecognizer *longpressgesture = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(longpressgesture:)];
//设置长按时间
longpressgesture.minimumpressduration = 0.5; //(2秒)
[self.view addgesturerecognizer:longpressgesture];
|
在对应的回调方法中添加相应的方法(当手势开始时执行):
1
2
3
4
5
6
7
8
9
10
|
//常摁手势触发方法
-( void )longpressgesture:(id)sender
{
uilongpressgesturerecognizer *longpress = sender;
if (longpress.state == uigesturerecognizerstatebegan)
{
uialertview *alter = [[uialertview alloc] initwithtitle:@ "提示" message:@ "长按触发" delegate:nil cancelbuttontitle:@ "取消" otherbuttontitles: nil];
[alter show];
}
}
|
代码说明:手势的常用状态如下
- 开始:uigesturerecognizerstatebegan
- 改变:uigesturerecognizerstatechanged
- 结束:uigesturerecognizerstateended
- 取消:uigesturerecognizerstatecancelled
- 失败:uigesturerecognizerstatefailed
3.轻扫手势(swipegesturerecognizer)
在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。
添加轻扫手势,一个向左一个向右,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
//添加轻扫手势
uiswipegesturerecognizer *swipegesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(swipegesture:)];
//设置轻扫的方向
swipegesture.direction = uiswipegesturerecognizerdirectionright; //默认向右
[self.view addgesturerecognizer:swipegesture];
//添加轻扫手势
uiswipegesturerecognizer *swipegestureleft = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(swipegesture:)];
//设置轻扫的方向
swipegestureleft.direction = uiswipegesturerecognizerdirectionleft; //默认向右
[self.view addgesturerecognizer:swipegestureleft];
|
回调方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//轻扫手势触发方法
-( void )swipegesture:(id)sender
{
uiswipegesturerecognizer *swipe = sender;
if (swipe.direction == uiswipegesturerecognizerdirectionleft)
{
//向左轻扫做的事情
}
if (swipe.direction == uiswipegesturerecognizerdirectionright)
{
//向右轻扫做的事情
}
}
|
4.捏合手势(pinchgesturerecognizer)
捏合手势初始化
1
2
3
|
//添加捏合手势
uipinchgesturerecognizer *pinchgesture = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(pinchgesture:)];
[self.view addgesturerecognizer:pinchgesture];
|
捏合手势要触发的方法(放大或者缩小图片):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
////捏合手势触发方法
-( void ) pinchgesture:(id)sender
{
uipinchgesturerecognizer *gesture = sender;
//手势改变时
if (gesture.state == uigesturerecognizerstatechanged)
{
//捏合手势中scale属性记录的缩放比例
_imageview.transform = cgaffinetransformmakescale(gesture.scale, gesture.scale);
}
//结束后恢复
if (gesture.state==uigesturerecognizerstateended)
{
[uiview animatewithduration:0.5 animations:^{
_imageview.transform = cgaffinetransformidentity; //取消一切形变
}];
}
}
|
5.拖动手势(pangesturerecognizer)
拖动手势的初始化
1
2
3
|
//添加拖动手势
uipangesturerecognizer *pangesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(pangesture:)];
[self.view addgesturerecognizer:pangesture];
|
拖动手势要做的方法(通过translationinview获取移动的点,和touchesmoved方法类似)
1
2
3
4
5
6
7
8
9
|
//拖动手势
-( void ) pangesture:(id)sender
{
uipangesturerecognizer *pangesture = sender;
cgpoint movepoint = [pangesture translationinview:self.view];
//做你想做的事儿
}
|
6.旋转手势(rotationgesturerecognizer)
旋转手势的初始化
1
2
3
|
//添加旋转手势
uirotationgesturerecognizer *rotationgesture = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(rotationgesture:)];
[self.view addgesturerecognizer:rotationgesture];
|
旋转手势调用的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//旋转手势
-( void )rotationgesture:(id)sender
{
uirotationgesturerecognizer *gesture = sender;
if (gesture.state==uigesturerecognizerstatechanged)
{
_imageview.transform=cgaffinetransformmakerotation(gesture.rotation);
}
if (gesture.state==uigesturerecognizerstateended)
{
[uiview animatewithduration:1 animations:^{
_imageview.transform=cgaffinetransformidentity; //取消形变
}];
}
}
|
上面的东西没有多高深的技术,就是对ios开发中的手势做了一下小小的总结,温故一下基础知识。在之前的博客中也有用到手势识别的内容,就是没有系统的梳理一下手势识别的知识,本文做一个基础的补充吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cocoachina.com/ios/20140922/9711.html