1. //
  2. //  ViewController.swift
  3. //  JieUITapGestureRecognizer
  4. //
  5. //  Created by jiezhang on 14-10-4.
  6. //  Copyright (c) 2014年 jiezhang. All rights reserved.
  7. //
  8. import UIKit
  9. class ViewController: UIViewController, UIActionSheetDelegate {
  10. @IBOutlet var im: UIImageView!
  11. var lastScaleFactor : CGFloat! = 1  //放大、缩小
  12. var netRotation : CGFloat = 1;//旋转
  13. var netTranslation : CGPoint!//平移
  14. var images : NSArray = ["meinv1.jpg","mv2.jpg","mv3.jpg","mv4.jpg","mv5.jpg","mv6.jpg"]// 图片数组
  15. var imageIndex : Int = 0 //数组下标
  16. required init(coder aDecoder: NSCoder) {
  17. super.init(coder: aDecoder)
  18. netTranslation = CGPoint(x: 0, y: 0)
  19. }
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. var tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
  23. //设置手势点击数,双击:点2下
  24. tapGesture.numberOfTapsRequired = 2
  25. self.view.addGestureRecognizer(tapGesture)
  26. //手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
  27. var pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:")
  28. self.view.addGestureRecognizer(pinchGesture)
  29. //旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
  30. var rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:")
  31. self.view.addGestureRecognizer(rotateGesture)
  32. //拖手势
  33. var panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
  34. //        self.view.addGestureRecognizer(panGesture)
  35. //划动手势
  36. //右划
  37. var swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
  38. self.view.addGestureRecognizer(swipeGesture)
  39. //左划
  40. var swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
  41. swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右
  42. self.view.addGestureRecognizer(swipeLeftGesture)
  43. //长按手势
  44. var longpressGesutre = UILongPressGestureRecognizer(target: self, action: "handleLongpressGesture:")
  45. //长按时间为1秒
  46. longpressGesutre.minimumPressDuration = 1
  47. //允许15秒运动
  48. longpressGesutre.allowableMovement = 15
  49. //所需触摸1次
  50. longpressGesutre.numberOfTouchesRequired = 1
  51. self.view.addGestureRecognizer(longpressGesutre)
  52. }
  53. override func didReceiveMemoryWarning() {
  54. super.didReceiveMemoryWarning()
  55. // Dispose of any resources that can be recreated.
  56. }
  57. //双击屏幕时会调用此方法,放大和缩小图片
  58. func handleTapGesture(sender: UITapGestureRecognizer){
  59. //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
  60. if im.contentMode == UIViewContentMode.ScaleAspectFit{
  61. //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
  62. im.contentMode = UIViewContentMode.Center
  63. }else{
  64. im.contentMode = UIViewContentMode.ScaleAspectFit
  65. }
  66. }
  67. //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
  68. func handlePinchGesture(sender: UIPinchGestureRecognizer){
  69. var factor = sender.scale
  70. if factor > 1{
  71. //图片放大
  72. im.transform = CGAffineTransformMakeScale(lastScaleFactor+factor-1, lastScaleFactor+factor-1)
  73. }else{
  74. //缩小
  75. im.transform = CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor)
  76. }
  77. //状态是否结束,如果结束保存数据
  78. if sender.state == UIGestureRecognizerState.Ended{
  79. if factor > 1{
  80. lastScaleFactor = lastScaleFactor + factor - 1
  81. }else{
  82. lastScaleFactor = lastScaleFactor * factor
  83. }
  84. }
  85. }
  86. //旋转手势
  87. func handleRotateGesture(sender: UIRotationGestureRecognizer){
  88. //浮点类型,得到sender的旋转度数
  89. var rotation : CGFloat = sender.rotation
  90. //旋转角度CGAffineTransformMakeRotation,改变图像角度
  91. im.transform = CGAffineTransformMakeRotation(rotation+netRotation)
  92. //状态结束,保存数据
  93. if sender.state == UIGestureRecognizerState.Ended{
  94. netRotation += rotation
  95. }
  96. }
  97. //拖手势
  98. func handlePanGesture(sender: UIPanGestureRecognizer){
  99. //得到拖的过程中的xy坐标
  100. var translation : CGPoint = sender.translationInView(im)
  101. //平移图片CGAffineTransformMakeTranslation
  102. im.transform = CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y)
  103. if sender.state == UIGestureRecognizerState.Ended{
  104. netTranslation.x += translation.x
  105. netTranslation.y += translation.y
  106. }
  107. }
  108. //划动手势
  109. func handleSwipeGesture(sender: UISwipeGestureRecognizer){
  110. //划动的方向
  111. var direction = sender.direction
  112. //判断是上下左右
  113. switch (direction){
  114. case UISwipeGestureRecognizerDirection.Left:
  115. println("Left")
  116. imageIndex++;//下标++
  117. break
  118. case UISwipeGestureRecognizerDirection.Right:
  119. println("Right")
  120. imageIndex--;//下标--
  121. break
  122. case UISwipeGestureRecognizerDirection.Up:
  123. println("Up")
  124. break
  125. case UISwipeGestureRecognizerDirection.Down:
  126. println("Down")
  127. break
  128. default:
  129. break;
  130. }
  131. //得到不越界不<0的下标
  132. imageIndex = imageIndex < 0 ? images.count-1:imageIndex%images.count
  133. //imageView显示图片
  134. im.image = UIImage(named: images[imageIndex] as String)
  135. }
  136. //长按手势
  137. func handleLongpressGesture(sender : UILongPressGestureRecognizer){
  138. if sender.state == UIGestureRecognizerState.Began{
  139. //创建警告
  140. var actionSheet = UIActionSheet(title: "Image options", delegate: self, cancelButtonTitle: "cancel", destructiveButtonTitle: "ok", otherButtonTitles: "other")
  141. actionSheet.showInView(self.view)
  142. }
  143. }
  144. }

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

Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)的更多相关文章

  1. iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer

    首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...

  2. iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer

    首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...

  3. Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍

    1.UITapGestureRecognizer 点击/双击手势 代码如下: var tapGesture = UITapGestureRecognizer(target: self, action: ...

  4. iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)       1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...

  5. ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...

  6. iOS基础 - 手势识别 与 手势说明

    一.使用手势识别的四个步骤 1> 实例化手势识别 - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> ...

  7. swift基础:第六部分:类与对象

    http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...

  8. swift基础:第二部分:函数和闭包

    今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...

  9. Swift基础语法学习总结(转)

    Swift基础语法学习总结 1.基础  1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...

随机推荐

  1. 项目学习——电力系统底层架构ssh

    电力系统底层架构1.建立web工程 创建数据库 导入向对应的jar包2. 持久层: (1)在cn.itcast.elec.domain中创建持久化类ElecText @SuppressWarnings ...

  2. JS编程常识

    一.UI层的松耦合 松耦合就是要求各层遵循“最少知识原则”,或者说是各层各司其职,不要越权: HTML:结构层 CSS:表现层 JS:行为层 对于各层的职能,有一句比较贴切的解释:HTML是名词(n) ...

  3. github心得

    心得  : 1:安装:省略 2. 配置 Git 以及上传代码 安装 Git 成功后,如果是 Windows 下,选择 Git Bash ,在命令行中完成一切,可能开始有点麻 烦,不过就那几条命令行,用 ...

  4. 在Visio2010中修改默认字体的大小

    由于我常需要在Visio2010中画流程图和UML图,但是Visio2010中的字体默认8px,这对眼睛是个挑战.摸索了好久终于找到在visio2010中修改字体大小的方式. 1.点一下红色箭头所指的 ...

  5. 读《你必须知道的.NET》继承本质论 Bird bird=new Chicken()

    我们创建如下的三层继承层次类. public abstract class Animal { public abstract void ShowType(); } public class Bird ...

  6. [ucgui] 对话框1——创建对话框

    >_<" 小工具和对话框的区别: 小工具可以创建并独立使用,因为它们本质上就是窗口.但是,通常需要使用对话框,它是包含一个或多个小工具的窗口. 对话框通常是一个窗口,它在出现时会 ...

  7. SQL Server FileStream

    以往我们对文件管理有两种方法: 数据库只保存文件的路径,具体的文件保存在文件服务器(NFS)上,使用时,编程实现从文件服务器读取文件: 将文件直接以varbinary(max)或image数据类型保存 ...

  8. Windows Azure 使用体验

    本文只是对Windows Azure的肤浅使用做个记录,算是简单入门吧. 一.门户网站 Windows Azure其实有两个版本,我们在国内所说的或者说所用的就是有别于国际版的,主要原因我想各位也是知 ...

  9. 1119 网页布局,css写下拉列表

    <style type="text/css"> *{ margin:0px; padding:0px;} #body{ width:1000px; height:200 ...

  10. atitit.信息安全的控制总结o7

    atitit.信息安全的控制总结o7 1. 信息安全覆盖很多的内容: 1 2. #内部人员导致的安全风险 1 3. #对敏感的数据进行透明的加密 2 4. #安全防护 2 5. #通过数据安全域保护关 ...