如何区分IOS上的pan和swipephotoswipe 手势缩放

记录一些工作或生活或爱好的地方
作者: 时间: July 28, 2014
在这次IOS应用开发教程中,我们打算实现手势识别。正如你所知道的,IOS支持大量的手势操作,它们能提供了很好的应用控制和出色用户体验。
让我们开始吧!
首先需要在Xcode中创建一个新的Single View Application:
然后点击Next,弹出的窗口要求你填写项目设置。在第一栏 (“Product name”) 中填入项目名称后,点击Next.
确保语言选择的是 “Swift”.
点击 “Main.storyboard” 文件,拖出6个 UIViews放到视图中.把视图排列成如图所示的样子.当你排列UIViews时,在每个view下面添加一个UILabel并依图设定文本值。
我们开始写代码吧.
是时候编辑实现文件了 (在我们的案例 “ViewController.swift” ).
为了声明一些我们将会用到的变量,要在 “class ViewController: UIViewController“块中添加如下代码.class ViewController: UIViewController {
@IBOutlet var tapView: UIView
@IBOutlet var swipeView: UIView
@IBOutlet var longPressView: UIView
@IBOutlet var pinchView: UIView
@IBOutlet var rotateView: UIView
@IBOutlet var panView: UIView
var lastRotation = CGFloat()
let tapRec = UITapGestureRecognizer()
let pinchRec = UIPinchGestureRecognizer()
let swipeRec = UISwipeGestureRecognizer()
let longPressRec = UILongPressGestureRecognizer()
let rotateRec = UIRotationGestureRecognizer()
let panRec = UIPanGestureRecognizer()
}在第2 – 7行,我们声明了在之前界面里排列过的 UIViews.
在第8行,我们声明了实现旋转手势要用到的变量(lastRotation).
在第 9 – 14行,我们为每个view声明了一个手势识别对象.
注意:&在 Swift中,我们用let关键字声明常量,这意味着它的值在程序运行时不可改变。关键字var则声明普通变量。
当声明完应用需要的主要变量后,在viewDidLoad&方法中添加如下代码.override func viewDidLoad() {
super.viewDidLoad()
tapRec.addTarget(self, action: "tappedView")
pinchRec.addTarget(self, action: "pinchedView:")
swipeRec.addTarget(self, action: "swipedView")
longPressRec.addTarget(self, action: "longPressedView")
rotateRec.addTarget(self, action: "rotatedView:")
panRec.addTarget(self, action: "draggedView:")
tapView.addGestureRecognizer(tapRec)
swipeView.addGestureRecognizer(swipeRec)
pinchView.addGestureRecognizer(pinchRec)
longPressView.addGestureRecognizer(longPressRec)
rotateView.addGestureRecognizer(rotateRec)
panView.addGestureRecognizer(panRec)
rotateView.userInteractionEnabled = true
rotateView.multipleTouchEnabled = true
pinchView.userInteractionEnabled = true
pinchView.multipleTouchEnabled = true
tapView.userInteractionEnabled = true
swipeView.userInteractionEnabled = true
longPressView.userInteractionEnabled = true
panView.userInteractionEnabled = true
}第 3 – 8行,为每个视图设定手势识别的目标。所谓的目标,就是每个view中的手势完成后要调用的方法。
第 9 -14行,把手势识别添加到视图中.
第15 – 22行,把每个视图的&userInteractionEnabled属性设为ture,并把拥有需要多点触控(rotateView&and&pinchView)的手势所在的视图的multipleTouchEnabled属性设为true.
现在,我们编写每个手势识别器要调用的方法 (第3 – 8行设置的目标方法 ).
添加如下代码:func tappedView(){
let tapAlert = UIAlertController(title: "Tapped", message: "You just tapped the tap view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
func swipedView(){
let tapAlert = UIAlertController(title: "Swiped", message: "You just swiped the swipe view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
func longPressedView(){
let tapAlert = UIAlertController(title: "Long Pressed", message: "You just long pressed the long press view", preferredStyle: UIAlertControllerStyle.Alert)
tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
self.presentViewController(tapAlert, animated: true, completion: nil)
}这三种方法都很好地完成同一件事.每次在手势在相应的视图中完成后,每种方法都弹出一个对话框.
所以&tappedView()方法在用户滑动视图时弹出一个对话框,swipedView()&方法在用户触摸滑动 swipe视图时弹出对话框,而longPressedView()&方法则在用户长按long press view时弹出对话框.
另两种手势 (rotate and pinch ) 的代码稍微有点复杂.
为旋转手势添加如下代码:func rotatedView(sender:UIRotationGestureRecognizer){
var lastRotation = CGFloat()
self.view.bringSubviewToFront(rotateView)
if(sender.state == UIGestureRecognizerState.Ended){
lastRotation = 0.0;
rotation = 0.0 - (lastRotation - sender.rotation)
var point = rotateRec.locationInView(rotateView)
var currentTrans = sender.view.transform
var newTrans = CGAffineTransformRotate(currentTrans, rotation)
sender.view.transform = newTrans
lastRotation = sender.rotation
}这个方法包含&sender:UIRotationGestureRecognizer参数. sender 参数(&UIRotationGestureRecognizer&类型) 含有这个方法(在这个案例中是rotateRec)调用的手势识别器的值.
第2行声明了&lastRotation.
第3行我们把&rotateView放到前面.
接下来,在 if语句中,我们检查手势是否完成,如果没有完成,我们就将视图旋转。
第 8 – 10行,我们计算rotate view的旋转程度,第10行,我们设置rotate view的旋转程度。
On line 12 we set thelastRotation&作为旋转手势识别器的当前旋转.
现在我们添加pinch 手势的代码:func pinchedView(sender:UIPinchGestureRecognizer){
self.view.bringSubviewToFront(pinchView)
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale)
sender.scale = 1.0
}在之前方法的第1行中,我们把pinch视图放到了顶端。然后设置每个pinch视图的transform,并把pinchRec的scale设为1.
然后是实现 pan (drag) 手势.&添加如下代码:func draggedView(sender:UIPanGestureRecognizer){
self.view.bringSubviewToFront(sender.view)
var translation = sender.translationInView(self.view)
sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y)
sender.setTranslation(CGPointZero, inView: self.view)
}第2行,我们把 drag视图放到顶端 (和前面的方法一样).
然后我们声明变量translation,并用sender.translationInView(self.view)的值给它赋值。 完成后,把sender.view object (panRec)&的center属性设为计算出来的新center &( 通过CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y)&计算) 并把translation 设为 sender (panRec).
现在,代码部分算是完成了!
回到界面设计.
现在我们回到 “Main.storyboard” 文件. 选择视图控制器并把声明的每个UIView连接到相应的视图,如下图所示.
现在你可以在模拟器或你的设备上运行该应用并测试手势。
附源代码:
添加新评论
SupperAdmin
: 哇哦,好专业的博客啊。
: 支持一个,文章写的很好
: 讲解的非常细致!学习啦!
: 看了博主的文章,感觉博主好流弊,懂的真多
: 谢谢,您的方法实现了!
: 一直停留在dos界面 请问怎么装的
: 总结不错,建议加上序号,条理会更清晰一些
: 嗯,非常实用
: 为什么收不到邀请邮件啊?被邀请账号需要怎么操作吗?
: 我发出了邀请,对方没有收到邮件,反复邀请,还是没有。为什么
: 谢谢分享 =^=
: 感谢博主。
: 多个域名绑定是不是也是这样,不会出现冲突的情况么
: /q/10100...
: 共享用samba,不共享直接ftp,svn等等都行。
: 我这里最新的实测结果是,邀请发出之后,被邀请人同意,然后半个小时...您的位置:
iOS开发教程之手势识别方法
日 10:35:49 | 作者:佚名 | 来源:51CTO
摘要:感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下...
感觉有必要把开发中的做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,也是目标动作回调,话不多说,切入今天的正题。总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势(SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer), 捏合手势(PinchGestureRecognizer),旋转手势(RotationGestureRecognizer);
其实这些手势用touche事件完全可以实现,苹果就是把常用的触摸事件封装成手势,来提供给用户。读者完全可以用TouchesMoved来写拖动手势等
一,用storyboard给控件添加手势识别,当然啦用storyboard得截张图啦
1.用storyboard添加手势识别,和添加一个Button的步骤一样,首先我们得找到相应的手势,把手势识别的控件拖到我们要添加手势的控件中,截图如下
498)this.width=498;' onmousewheel = 'javascript:return big(this)' width="480" height="259" border="0" src="w.cn/resources/4_10_16/50_6082.png" alt="" />
2.给我们拖出的手势添加回调事件,和给Button回调事件没啥区别的,在回调方法中添加要实现的业务逻辑即可,截图如下:
498)this.width=498;' onmousewheel = 'javascript:return big(this)' width="486" height="52" border="0" src="w.cn/resources/4_10_16/50_5852.png" alt="" />
二,纯代码添加手势识别
用storyboard可以大大简化我们的操作(+微信关注网络世界),不过纯代码的方式还是要会的,就像 要Dreamwear编辑网页一样(当然啦,storyboard的拖拽功能要比Dreamwear的拖拽强大的多),用纯代码敲出来的更为灵活,更加便 于维护。不过用storyboard可以减少我们的工作量,这两个要配合着使用才能大大的提高我们的开发效率。个人感觉用storyboard把框架搭起 来(Controller间的关系),一下小的东西还是用纯代码敲出来更好一些。下面就给出如何给我们的控件用纯代码的方式来添加手势识别。
1.轻击手势(TapGestureRecognizer)的添加
初始化代码TapGestureRecongnizer的代码如下:
1&&&&&//新建tap手势&3&&&&&//设置点击次数和点击手指数&5&&&&&tapGesture.numberOfTouchesRequired&=&1;&//点击手指数&
在回调方法中添加相应的业务逻辑:
1&//轻击手势触发方法&3&{&5&}&
2.长按手势(LongPressGestureRecognizer)
初始化代码:
//添加长摁手势&3&&&&&//设置长按时间&5&&&&&[self.view&addGestureRecognizer:longPressGesture];&
在对应的回调方法中添加相应的方法(当手势开始时执行):
1&//常摁手势触发方法&3&{&5&&&&&if&(longPress.state&==&UIGestureRecognizerStateBegan)&7&&&&&&&&&UIAlertView&*alter&=&[[UIAlertView&alloc]&initWithTitle:@"提示"&message:@"长按触发"&delegate:nil&cancelButtonTitle:@"取消"&otherButtonTitles:&nil];&9&&&&&}&
代码说明:手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
3.轻扫手势(SwipeGestureRecognizer)
在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。
添加轻扫手势,一个向左一个向右,代码如下:
1&&&&&//添加轻扫手势&&3&&&&&//设置轻扫的方向&&5&&&&&[self.view&addGestureRecognizer:swipeGesture];&&7&&&&&//添加轻扫手势&&9&&&&&//设置轻扫的方向&11&&&&&[self.view&addGestureRecognizer:swipeGestureLeft];&
回调方法如下:
1&//轻扫手势触发方法&&3&{&&5&&&&&if&(swipe.direction&==&UISwipeGestureRecognizerDirectionLeft)&&7&&&&&&&&&//向左轻扫做的事情&&9&&&&&if&(swipe.direction&==&UISwipeGestureRecognizerDirectionRight)&11&&&&&&&&&//向右轻扫做的事情&13&}&
4.捏合手势(PinchGestureRecognizer)
捏合手势初始化
&&//添加捏合手势&3&&&&&[self.view&addGestureRecognizer:pinchGesture];&
捏合手势要触发的方法(放大或者缩小图片):
&1&////捏合手势触发方法&&3&{&&5&&&&&&&7&&&&&if&(gesture.state&==&UIGestureRecognizerStateChanged)&&9&&&&&&&&&//捏合手势中scale属性记录的缩放比例&11&&&&&}&13&&&&&//结束后恢复&15&&&&&{&17&&&&&&&&&&&&&_imageView.transform&=&CGAffineTransformI//取消一切形变&19&&&&&}&
5.拖动手势(PanGestureRecognizer)
拖动手势的初始化
//添加拖动手势&3&&&&&[self.view&addGestureRecognizer:panGesture];&
拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)
1&//拖动手势&3&{&5&&&&&&7&&&&&&9&}&
6.旋转手势(RotationGestureRecognizer)
旋转手势的初始化
&//添加旋转手势&3&&&&&[self.view&addGestureRecognizer:rotationGesture];&
旋转手势调用的方法:
复制代码&&1&//旋转手势&&3&{&&5&&&&&UIRotationGestureRecognizer&*gesture&=&&&7&&&&&if&(gesture.state==UIGestureRecognizerStateChanged)&&9&&&&&&&&&_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);&11&&&&&&13&&&&&{&15&&&&&&&&&[UIView&animateWithDuration:1&animations:^{&17&&&&&&&&&}];&19&&&&&&
本文链接:/ludashi/p/3983008.html
[责任编辑:孙可 ]
正在加载...
我也说几句
热点排行周月
网络世界移动客户端网界网微信订阅号iOS开发之手势识别汇总--百度百家
iOS开发之手势识别汇总
分享到微信朋友圈
iOS开发中手势识别有六种:
轻击手势(TapGestureRecognizer),
轻扫手势(SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
iOS开发中手势识别有六种:
轻击手势(TapGestureRecognizer),
轻扫手势(SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
1,轻击手势(TapGestureRecognizer)
UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired=1;//点击次数
tapGesture.numberOfTouchesRequired=1;//点击手指数
[self.viewaddGestureRecognizer:tapGesture];
//轻击手势触发方法
-(void)tapGesture:(UITapGestureRecognizer*)sender
//yourcode
2,长按手势(LongPressGestureRecognizer)
UILongPressGestureRecognizer*longPressGesture=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration=0.5;
[self.viewaddGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
UILongPressGestureRecognizer*longPress=
if(longPress.state==UIGestureRecognizerStateBegan)
//yourcode
说明:长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
3,轻扫手势(SwipeGestureRecognizer)
UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction=UISwipeGestureRecognizerDirectionR//向右
[self.viewaddGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer*swipeGestureLeft=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction=UISwipeGestureRecognizerDirectionL//向左
[self.viewaddGestureRecognizer:swipeGestureLeft];
//轻扫手势触发方法
-(void)swipeGesture:(id)sender
UISwipeGestureRecognizer*swipe=
if(swipe.direction==UISwipeGestureRecognizerDirectionLeft)
//向左轻扫
if(swipe.direction==UISwipeGestureRecognizerDirectionRight)
//向右轻扫
4,捏合手势(PinchGestureRecognizer)
UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchGesture:)];
[self.viewaddGestureRecognizer:pinchGesture];
////捏合手势触发方法
-(void)pinchGesture:(id)sender
UIPinchGestureRecognizer*gesture=
//手势改变时
if(gesture.state==UIGestureRecognizerStateChanged)
//捏合手势中scale属性记录的缩放比例
_imageView.transform=CGAffineTransformMakeScale(gesture.scale,gesture.scale);
//结束后恢复
if(gesture.state==UIGestureRecognizerStateEnded)
[UIViewanimateWithDuration:0.5animations:^{
_imageView.transform=CGAffineTransformI//取消一切形变
5,拖动手势(PanGestureRecognizer)
UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panGesture:)];
[self.viewaddGestureRecognizer:panGesture];
//拖动手势触发方法
-(void)panGesture:(id)sender
UIPanGestureRecognizer*panGesture=
CGPointmovePoint=[panGesturetranslationInView:self.view];
//yourcode
6,旋转手势(RotationGestureRecognizer)
UIRotationGestureRecognizer*rotationGesture=[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationGesture:)];
[self.viewaddGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
UIRotationGestureRecognizer*gesture=
if(gesture.state==UIGestureRecognizerStateChanged)
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
if(gesture.state==UIGestureRecognizerStateEnded)
[UIViewanimateWithDuration:1animations:^{
_imageView.transform=CGAffineTransformI//取消形变
作者李刚是刚刚在线(&&)站长,百度百家专栏作者
iOS工程师非著名自媒体,微信公众号iOS开发:iOSDevTip运营者
分享到微信朋友圈
在手机阅读、分享本文
还可以输入250个字
推荐文章RECOMMEND
阅读:5895
阅读:3423
阅读:15万
阅读:11万
热门文章HOT NEWS
正像那些年传唱过的神曲一样,只不过那些神曲只能嘴上唱唱,说不定...
品牌建筑师
中欧国际工商学院
百度新闻客户端
百度新闻客户端
百度新闻客户端
扫描二维码下载
订阅 "百家" 频道
观看更多百家精彩新闻}

我要回帖

更多关于 ios pan手势左右滑动 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信