Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)

  1. //
  2. //ViewController.swift
  3. //JieUITapGestureRecognizer
  4. //
  5. //Createdbyjiezhangon14-10-4.
  6. //Copyright(c)2014年jiezhang.Allrightsreserved.
  7. importUIKit
  8. classViewController:UIViewController,UIActionSheetDelegate{
  9. @IBOutletvarim:UIImageView!
  10. varlastScaleFactor:CGFloat!=1//放大、缩小
  11. varnetRotation:CGFloat=1;//旋转
  12. varnetTranslation:CGPoint!//平移
  13. varimages:NSArray=["meinv1.jpg","mv2.jpg","mv3.jpg","mv4.jpg","mv5.jpg","mv6.jpg"]//图片数组
  14. varimageIndex:Int=0//数组下标
  15. requiredinit(coderaDecoder:NSCoder){
  16. super.init(coder:aDecoder)
  17. netTranslation=CGPoint(x:0,y:0)
  18. }
  19. overridefuncviewDidLoad(){
  20. super.viewDidLoad()
  21. vartapGesture=UITapGestureRecognizer(target:self,0); background-color:inherit">action:"handleTapGesture:")
  22. //设置手势点击数,双击:点2下
  23. tapGesture.numberOfTapsrequired=2
  24. self.view.addGestureRecognizer(tapGesture)
  25. //手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
  26. varpinchGesture=UIPinchGestureRecognizer(target:self,0); background-color:inherit">action:"handlePinchGesture:")
  27. self.view.addGestureRecognizer(pinchGesture)
  28. //旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
  29. varrotateGesture=UIRotationGestureRecognizer(target:self,0); background-color:inherit">action:"handleRotateGesture:")
  30. self.view.addGestureRecognizer(rotateGesture)
  31. //拖手势
  32. varpanGesture=UIPanGestureRecognizer(target:self,0); background-color:inherit">action:"handlePanGesture:")
  33. //self.view.addGestureRecognizer(panGesture)
  34. //划动手势
  35. //右划
  36. varswipeGesture=UISwipeGestureRecognizer(target:self,0); background-color:inherit">action:"handleSwipeGesture:")
  37. self.view.addGestureRecognizer(swipeGesture)
  38. //左划
  39. varswipeLeftGesture=UISwipeGestureRecognizer(target:self,0); background-color:inherit">action:"handleSwipeGesture:")
  40. swipeLeftGesture.direction=UISwipeGestureRecognizerDirection.Left//不设置是右
  41. self.view.addGestureRecognizer(swipeLeftGesture)
  42. //长按手势
  43. varlongpressGesutre=UILongPressGestureRecognizer(target:self,0); background-color:inherit">action:"handleLongpressGesture:")
  44. //长按时间为1秒
  45. longpressGesutre.minimumPressDuration=1
  46. //允许15秒运动
  47. longpressGesutre.allowableMovement=15
  48. //所需触摸1次
  49. longpressGesutre.numberOfTouchesrequired=1
  50. self.view.addGestureRecognizer(longpressGesutre)
  51. }
  52. overridefuncdidReceiveMemoryWarning(){
  53. super.didReceiveMemoryWarning()
  54. //DispoSEOfanyresourcesthatcanberecreated.
  55. //双击屏幕时会调用方法,放大和缩小图片
  56. funchandleTapGesture(sender:UITapGestureRecognizer){
  57. //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
  58. ifim.contentMode==UIViewContentMode.ScaleAspectFit{
  59. //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
  60. im.contentMode=UIViewContentMode.Center
  61. }else{
  62. im.contentMode=UIViewContentMode.ScaleAspectFit
  63. //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
  64. funchandlePinchGesture(sender:UIPinchGestureRecognizer){
  65. varfactor=sender.scale
  66. iffactor>1{
  67. //图片放大
  68. im.transform=CGAffineTransformMakeScale(lastScaleFactor+factor-1,lastScaleFactor+factor-1)
  69. }else{
  70. //缩小
  71. im.transform=CGAffineTransformMakeScale(lastScaleFactor*factor,lastScaleFactor*factor)
  72. //状态是否结束,如果结束保存数据
  73. ifsender.state==UIGestureRecognizerState.Ended{
  74. iffactor>1{
  75. lastScaleFactor=lastScaleFactor+factor-1
  76. lastScaleFactor=lastScaleFactor*factor
  77. //旋转手势
  78. funchandleRotateGesture(sender:UIRotationGestureRecognizer){
  79. //浮点类型,得到sender的旋转度数
  80. varrotation:CGFloat=sender.rotation
  81. //旋转角度CGAffineTransformMakeRotation,改变图像角度
  82. im.transform=CGAffineTransformMakeRotation(rotation+netRotation)
  83. //状态结束,保存数据
  84. netRotation+=rotation
  85. //拖手势
  86. funchandlePanGesture(sender:UIPanGestureRecognizer){
  87. //得到拖的过程中的xy坐标
  88. vartranslation:CGPoint=sender.translationInView(im)
  89. //平移图片CGAffineTransformMakeTranslation
  90. im.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x,netTranslation.y+translation.y)
  91. netTranslation.x+=translation.x
  92. netTranslation.y+=translation.y
  93. funchandleSwipeGesture(sender:UISwipeGestureRecognizer){
  94. //划动的方向
  95. vardirection=sender.direction
  96. //判断是上下左右
  97. switch(direction){
  98. caseUISwipeGestureRecognizerDirection.Left:
  99. println("Left")
  100. imageIndex++;//下标++
  101. break
  102. caseUISwipeGestureRecognizerDirection.Right:
  103. println("Right")
  104. imageIndex--;//下标--
  105. caseUISwipeGestureRecognizerDirection.Up:
  106. println("Up")
  107. break
  108. caseUISwipeGestureRecognizerDirection.Down:
  109. println("Down")
  110. default:
  111. break;
  112. //得到不越界不<0的下标
  113. imageIndex=imageIndex<0?images.count-1:imageIndex%images.count
  114. //imageView显示图片
  115. im.image=UIImage(named:images[imageIndex]asString)
  116. funchandleLongpressGesture(sender:UILongPressGestureRecognizer){
  117. ifsender.state==UIGestureRecognizerState.Began{
  118. //创建警告
  119. varactionSheet=UIActionSheet(title:"Imageoptions",0); background-color:inherit">delegate:self,0); background-color:inherit">cancelButtonTitle:"cancel",0); background-color:inherit">destructiveButtonTitle:"ok",0); background-color:inherit">otherButtonTitles:"other")
  120. actionSheet.showInView(self.view)
  121. }


注意:滑动手势和拖手势冲突,两个选一个测试,至于测试的效果自己新建一个工程来把代码加入就行

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...