博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
阅读量:5102 次
发布时间:2019-06-13

本文共 6814 字,大约阅读时间需要 22 分钟。

/      //  ViewController.swift      //  JieUITapGestureRecognizer      //      //  Created by jiezhang on 14-10-4.      //  Copyright (c) 2014年 jiezhang. All rights reserved.      //            import UIKit            class ViewController: UIViewController, UIActionSheetDelegate {                    @IBOutlet var im: UIImageView!          var lastScaleFactor : CGFloat! = 1  //放大、缩小          var netRotation : CGFloat = 1;//旋转          var netTranslation : CGPoint!//平移          var images : NSArray = ["meinv1.jpg","mv2.jpg","mv3.jpg","mv4.jpg","mv5.jpg","mv6.jpg"]// 图片数组          var imageIndex : Int = 0 //数组下标                          required init(coder aDecoder: NSCoder) {              super.init(coder: aDecoder)              netTranslation = CGPoint(x: 0, y: 0)          }                    override func viewDidLoad() {              super.viewDidLoad()                                        var tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")              //设置手势点击数,双击:点2下              tapGesture.numberOfTapsRequired = 2              self.view.addGestureRecognizer(tapGesture)                            //手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上              var pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:")              self.view.addGestureRecognizer(pinchGesture)                            //旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上              var rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:")              self.view.addGestureRecognizer(rotateGesture)                            //拖手势              var panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")      //        self.view.addGestureRecognizer(panGesture)                            //划动手势              //右划              var swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")              self.view.addGestureRecognizer(swipeGesture)              //左划              var swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")              swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left //不设置是右              self.view.addGestureRecognizer(swipeLeftGesture)                            //长按手势              var longpressGesutre = UILongPressGestureRecognizer(target: self, action: "handleLongpressGesture:")              //长按时间为1秒              longpressGesutre.minimumPressDuration = 1              //允许15秒运动              longpressGesutre.allowableMovement = 15              //所需触摸1次              longpressGesutre.numberOfTouchesRequired = 1              self.view.addGestureRecognizer(longpressGesutre)          }                override func didReceiveMemoryWarning() {              super.didReceiveMemoryWarning()              // Dispose of any resources that can be recreated.          }                    //双击屏幕时会调用此方法,放大和缩小图片          func handleTapGesture(sender: UITapGestureRecognizer){              //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小               if im.contentMode == UIViewContentMode.ScaleAspectFit{                  //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView                 im.contentMode = UIViewContentMode.Center              }else{                  im.contentMode = UIViewContentMode.ScaleAspectFit              }          }                    //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作          func handlePinchGesture(sender: UIPinchGestureRecognizer){              var factor = sender.scale              if factor > 1{                  //图片放大                  im.transform = CGAffineTransformMakeScale(lastScaleFactor+factor-1, lastScaleFactor+factor-1)              }else{                  //缩小                  im.transform = CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor)              }              //状态是否结束,如果结束保存数据              if sender.state == UIGestureRecognizerState.Ended{                  if factor > 1{                      lastScaleFactor = lastScaleFactor + factor - 1                  }else{                      lastScaleFactor = lastScaleFactor * factor                  }              }          }                    //旋转手势          func handleRotateGesture(sender: UIRotationGestureRecognizer){              //浮点类型,得到sender的旋转度数               var rotation : CGFloat = sender.rotation              //旋转角度CGAffineTransformMakeRotation,改变图像角度              im.transform = CGAffineTransformMakeRotation(rotation+netRotation)              //状态结束,保存数据              if sender.state == UIGestureRecognizerState.Ended{                  netRotation += rotation              }          }          //拖手势          func handlePanGesture(sender: UIPanGestureRecognizer){              //得到拖的过程中的xy坐标              var translation : CGPoint = sender.translationInView(im)              //平移图片CGAffineTransformMakeTranslation              im.transform = CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y)              if sender.state == UIGestureRecognizerState.Ended{                  netTranslation.x += translation.x                  netTranslation.y += translation.y              }          }          //划动手势          func handleSwipeGesture(sender: UISwipeGestureRecognizer){              //划动的方向              var direction = sender.direction              //判断是上下左右              switch (direction){              case UISwipeGestureRecognizerDirection.Left:                  println("Left")                  imageIndex++;//下标++                  break              case UISwipeGestureRecognizerDirection.Right:                  println("Right")                  imageIndex--;//下标--                  break              case UISwipeGestureRecognizerDirection.Up:                  println("Up")                  break              case UISwipeGestureRecognizerDirection.Down:                  println("Down")                  break              default:                  break;              }              //得到不越界不<0的下标              imageIndex = imageIndex < 0 ? images.count-1:imageIndex%images.count              //imageView显示图片              im.image = UIImage(named: images[imageIndex] as String)          }                    //长按手势          func handleLongpressGesture(sender : UILongPressGestureRecognizer){                            if sender.state == UIGestureRecognizerState.Began{                  //创建警告                  var actionSheet = UIActionSheet(title: "Image options", delegate: self, cancelButtonTitle: "cancel", destructiveButtonTitle: "ok", otherButtonTitles: "other")                  actionSheet.showInView(self.view)              }          }      }   注意:滑动手势和拖手势冲突,两个选一个测试,至于测试的效果自己新建一个工程来把代码加入就行

 

转载于:https://www.cnblogs.com/Free-Thinker/p/5329272.html

你可能感兴趣的文章
AndroidStudio中获得的VersionCode一直为1和VersionName一直为1.0
查看>>
【搜索引擎基础知识1】搜索引擎基本架构 2014-05-23 16:00 568人阅读 评论(0) 收藏...
查看>>
POJ1144 Network 连通性
查看>>
shell脚本中的日期处理
查看>>
HDU 1754 I Hate It
查看>>
【python】-- Django ModelForm
查看>>
响应式网页设计
查看>>
self-hacking第五天
查看>>
MySQL 显示版本、端口、状态
查看>>
8 -- 深入使用Spring -- 2... Spring的“零配置”支持
查看>>
C#编程:SqlCommand.Parameters.Add()方法的参数问题。
查看>>
python 小技巧之获取固定下面包含的某种类型文件的个数
查看>>
ReentrantLock 源码解析
查看>>
永远的动漫,梦想在,就有远方
查看>>
mysql 查看当前连接及修改连接数
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
【Luogu1303】【模板】A*B Problem
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>
HTML——校友会(bootstrap)
查看>>
【分布计算环境学习笔记】2 分布式系统中的面向对象技术
查看>>