Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
- //
- // 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)
- }
- }
- }
注意:滑动手势和拖手势冲突,两个选一个测试,至于测试的效果自己新建一个工程来把代码加入就行
Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)的更多相关文章
- iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...
- iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
1.UITapGestureRecognizer 点击/双击手势 代码如下: var tapGesture = UITapGestureRecognizer(target: self, action: ...
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
- iOS基础 - 手势识别 与 手势说明
一.使用手势识别的四个步骤 1> 实例化手势识别 - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> ...
- swift基础:第六部分:类与对象
http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...
- swift基础:第二部分:函数和闭包
今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...
- Swift基础语法学习总结(转)
Swift基础语法学习总结 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...
随机推荐
- Cocos2d-x Application Wizard for Visual Studio User Guide
0. Overview Cocos2d-x-win32's project can be generated by Wizard. Wizard supports Visual Studio 2008 ...
- 12 个 Web 设计师必备的 Bootstrap 工具
转自:http://www.oschina.net/translate/12-best-bootstrap-tools-for-web-designers Bootstrap是一个非常棒的前端网站开发 ...
- xps 文件操作笔记
1. 在 Silverlight 显示XPS文件,参考:http://azharthegreat.codeplex.com/ 2. Word,Excel, PPT 文件转换为XPS: 参考一(老外写的 ...
- Python time datetime常用时间处理方法
常用时间转换及处理函数: import datetime # 获取当前时间 d1 = datetime.datetime.now() print d1 # 当前时间加上半小时 d2 = d1 + da ...
- 团队项目--站立会议 DAY2
小组名称:D&M 参会人员:张靖颜,钟灵毓秀,何玥,赵莹,王梓萱 项目进展: 1,张靖颜:进行了对需求的分析,将项目框架搭建进行完善,辅助编写代码. 2,钟灵毓秀:相关功能的代码的完善,进行一 ...
- DDD领域驱动设计之运用层代码
1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 4.DDD领域驱动设计之领域服务 5.整体DEMO代码 什么是运用层,说 ...
- paip.Java Annotation注解的作用and 使用
paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...
- iOS开发——高级技术&PassBook服务
PassBook服务 Passbook是苹果推出的一个管理登机牌.会员卡.电影票.优惠券等信息的 工具.Passbook就像一个卡包,用于存放你的购物卡.积分卡.电影票.礼品卡等,而这些票据就是一个“ ...
- Spring MVC 文件上传下载
本文基于Spring MVC 注解,让Spring跑起来. (1) 导入jar包:ant.jar.commons-fileupload.jar.connom-io.jar. (2) 在src/cont ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...