触摸是一个UITouch对象,该对象放在一个UIEvent中,每个UIEvent包含一个或多个UITouch对象,每个UITouch对象对应一个手指。系统将UIEvent发送到应用程序上,最后应用程序将UIEvent传递给当前的一个UIView。

触摸分为5个阶段:

1)Began

2)Moved

3)Stationary

4)Ended

5)Cancelled(比如收到电话)

//触摸事件

    //手指首次触摸到屏幕
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { print("touchesBegan") //获取touches数量
let numTouches = touches.count //获取点击屏幕的次数
let tapTouches = (touches as NSSet).anyObject()?.tapCount //获取事件发生时间
let timestamp = event!.timestamp //获取当前相对于self.view的坐标
let locationPoint = (touches as NSSet).anyObject()?.locationInView(self.view) //获取上一次相对于self.view的坐标
let previousPoint = (touches as NSSet).anyObject()?.previousLocationInView(self.view) //允许使用手势
self.view.userInteractionEnabled = true //支持多点触摸
self.view.multipleTouchEnabled = true print("\(tapTouches)") //判断如果有两个触摸点
if touches.count ==
{
//获取触摸集合
let twoTouches = (touches as NSSet).allObjects //获取触摸数组
let first:UITouch = twoTouches[] as! UITouch //第1个触摸点
let second:UITouch = twoTouches[]as! UITouch //第2个触摸点 //获取第1个点相对于self.view的坐标
let firstPoint:CGPoint = first.locationInView(self.view) //获取第1个点相对于self.view的坐标
let secondPoint:CGPoint = second.locationInView(self.view) //计算两点之间的距离
let deltaX = secondPoint.x - firstPoint.x;
let deltaY = secondPoint.y - firstPoint.y;
let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY ) print("两点间距离是:\(initialDistance)")
}
} //手指在移动
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { print("touchesMoved")
} //触摸结束
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { print("touchesEnded")
} //触摸意外终止
//模拟器演示:鼠标拖动的同时,按键盘command+shift+h 相当于点击手机home键,退出应用,触发touchesCancelled事件,在打电话、等情况下也会触发
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { print("touchesCancelled")
}

手势UIGestureRecognizer:

UITapGestureRecognizer

UIPanGestureRecognizer

UILongPressGestureRecognizer

UIPinchGestureRecognizer

UISwipeGestureRecognizer

        //点击事件
let atap = UITapGestureRecognizer(target: self, action: "tapDo:")
self.view.addGestureRecognizer(atap)
atap.numberOfTapsRequired = //单击次数
atap.numberOfTouchesRequired = //手指个数 //拖动事件
let aPan = UIPanGestureRecognizer(target: self, action: "handlenPan:")
self.view.addGestureRecognizer(aPan)
aPan.minimumNumberOfTouches = //最少手指个数
aPan.maximumNumberOfTouches = //最多手指个数 //长按事件
let aLongPress = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(aLongPress)
aLongPress.minimumPressDuration = //需要长按的时间,最小0.5s //捏合事件
let aPinch = UIPinchGestureRecognizer(target: self, action: "pinchDo:")
self.view.addGestureRecognizer(aPinch) //旋转事件
let aRotation = UIRotationGestureRecognizer(target: self, action: "rotatePiece:")
self.view.addGestureRecognizer(aRotation) //轻扫事件--左轻扫
let leftSwipe = UISwipeGestureRecognizer(target: self, action: "leftSwipe:")
self.view.addGestureRecognizer(leftSwipe)
leftSwipe.direction = UISwipeGestureRecognizerDirection.Left //轻扫事件--右轻扫
let rightSwipe = UISwipeGestureRecognizer(target: self, action: "rightSwipe:")
self.view.addGestureRecognizer(rightSwipe)
rightSwipe.direction = UISwipeGestureRecognizerDirection.Right //轻扫事件--上轻扫
let upSwipe = UISwipeGestureRecognizer(target: self, action: "upSwipe:")
self.view.addGestureRecognizer(upSwipe)
upSwipe.direction = UISwipeGestureRecognizerDirection.Up //轻扫事件--下轻扫
let downSwipe = UISwipeGestureRecognizer(target: self, action: "downSwipe:")
self.view.addGestureRecognizer(downSwipe)
downSwipe.direction = UISwipeGestureRecognizerDirection.Down
 //手势

     //点击事件
func tapDo(sender:UITapGestureRecognizer)
{ print("点击事件")
} //拖动事件
func handlenPan(sender:UIPanGestureRecognizer)
{
print("拖动事件") if sender.state == .Began
{
//拖动开始
}
else if sender.state == .Changed
{
//拖动过程
}
else if sender.state == .Ended
{
//拖动结束
}
} //长摁事件
func longPress(sender:UILongPressGestureRecognizer)
{
print("长摁事件") } //捏合事件
func pinchDo(sender:UIPinchGestureRecognizer)
{
print("捏合")
} //旋转事件
func rotatePiece(sender:UIRotationGestureRecognizer)
{
print("旋转")
} //轻扫事件--左轻扫
func leftSwipe(sender:UISwipeGestureRecognizer)
{
print("左轻扫")
} //轻扫事件--右轻扫
func rightSwipe(sender:UISwipeGestureRecognizer)
{
print("右轻扫")
} //轻扫事件--上轻扫
func upSwipe(sender:UISwipeGestureRecognizer)
{
print("上轻扫")
} //轻扫事件--下轻扫
func downSwipe(sender:UISwipeGestureRecognizer)
{
print("下轻扫")
}

触摸与手势学习-swift的更多相关文章

  1. T470 Win10下触摸板手势

    T470 Win10下触摸板手势 学习了:https://forum.51nb.com/thread-1742490-1-1.html 三指横向竟然是alt+tab 学习了:http://www.xi ...

  2. Javascript高级编程学习笔记(69)—— 事件(13)触摸与手势事件

    触摸与手势事件 由于移动设备既没有鼠标也没有键盘,所以在为移动浏览器开发交互性网页时,常规的鼠标键盘事件根本不够用 所以早期的苹果为Safari 添加了一些与触摸相关的事件 随着后面Android的W ...

  3. IOS 手势-轻点、触摸、手势、事件

    1.概念 手势是从你用一个或多个手指接触屏幕时开始,直到手指离开屏幕为止所发生的所有事件.无论手势持续多长时间,只要一个或多个手指仍在屏幕上,这个手势就存在. 触摸是指把手指放到IOS设备的屏幕上,从 ...

  4. ios -- 教你如何轻松学习Swift语法(三) 完结篇

    前言:swift语法基础篇(二)来了,想学习swift的朋友可以拿去参考哦,有兴趣可以相互探讨,共同学习哦.      一.自动引用计数   1.自动引用计数工作机制      1.1 swift和o ...

  5. ios -- 教你如何轻松学习Swift语法(二)

    前言:swift语法基础篇(二)来了,想学习swift的朋友可以拿去参考哦,有兴趣可以相互探讨,共同学习哦.      一.可选类型(重点内容)   1.什么是可选类型?        1.1在OC开 ...

  6. ios -- 教你如何轻松学习Swift语法(一)

    目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...

  7. 一步一步学习Swift之(一):关于swift与开发环境配置

    一.什么是Swift? 1.Swift 是一种新的编程语言,用于编写 iOS 和 OS X 应用. 2.Swift 结合了 C 和 Objective-C 的优点并且不受 C 兼容性的限制. 3.Sw ...

  8. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

  9. JavaScript触摸与手势事件

    JavaScript触摸与手势事件 发表于 2012-12-10 由 admin iOS版Safari为了向开发人员传达一些特殊信息,新增了一些专有事件.因为iOS设备既没有鼠标也没有键盘,所以在为移 ...

随机推荐

  1. 【图片处理】cocos2dx png图片压缩处理

    一.介绍 美术用photoshop出图有时候会包含一些无用的信息,这时候image magick可以把这些信息裁掉. 二.使用方法 1.下载并安装Image Magick 2.将脚本里的目录名改成Im ...

  2. 【Linux】理解setuid()、setgid()和sticky位

    详见: http://blog.csdn.net/m13666368773/article/details/7615125 Linux SETUID机制 (1)进程运行时能够访问哪些资源或文件,不取决 ...

  3. 【HDOJ】1540 Tunnel Warfare

    还不错的一道线段树区间合并.挺巧妙的用法. /* 1540 */ #include <iostream> #include <string> #include <map& ...

  4. Form – 保存自動關閉當前窗口

    FAQ: 在BUTTON的触发器中,写如下代码, commit_form; go_bloack('你想显示的那个window的block'); --或者写 show_view('你要显示的canvas ...

  5. Android开发UI之控件-Android-PullToRefresh

    下拉刷新,使用的是Android-PullToRefresh,Github--https://github.com/chrisbanes/Android-PullToRefresh PullToRef ...

  6. hdu2852KiKi's K-Number(区间K值)

    http://acm.hdu.edu.cn/showproblem.php?pid=2852 区间K值写错了... #include <iostream> #include<cstd ...

  7. POJ2104 区间第k小

    题意就是区间第k大…… 题解: 前段时间用主席树搞掉了…… 如今看到划分树,是在想来写一遍,结果18号对着学长的代码调了一上午连样例都没过,好桑心…… 今天在做NOI2010超级钢琴,忽然发现用划分树 ...

  8. CodeForces 450

    A - Jzzhu and Children Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  9. 【转】Windows7打造全方位护眼系统

    原文网址:http://www.cnblogs.com/duboway/archive/2013/04/20/3033257.html 电脑屏幕: Win7和Vista系统设置如下: 第一步:桌面空白 ...

  10. WCF 绑定(Binding)

    绑定包含多个绑定元素 ,它 们描述了所有绑定要求 .可以创建自定义绑定 ,也可以使用下表中的其中一个预定义绑定 : 不同的绑定支持不同的功能.以Ws开头的绑定独立于平台 ,支持 Web服务规范. 以 ...