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

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


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

猜你在找的Swift相关文章