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

前端之家收集整理的这篇文章主要介绍了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相关文章