触摸是一个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 == 2
{
//获取触摸集合
let twoTouches = (touches as NSSet).allObjects
//获取触摸数组
let first:UITouch = twoTouches[0] as! UITouch //第1个触摸点
let second:UITouch = twoTouches[1]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
1 //点击事件
2 let atap = UITapGestureRecognizer(target: self, action: "tapDo:")
3 self.view.addGestureRecognizer(atap)
4 atap.numberOfTapsRequired = 1 //单击次数
5 atap.numberOfTouchesRequired = 1 //手指个数
6
7 //拖动事件
8 let aPan = UIPanGestureRecognizer(target: self, action: "handlenPan:")
9 self.view.addGestureRecognizer(aPan)
10 aPan.minimumNumberOfTouches = 1 //最少手指个数
11 aPan.maximumNumberOfTouches = 3 //最多手指个数
12
13 //长按事件
14 let aLongPress = UILongPressGestureRecognizer(target: self, action: "longPress:")
15 self.view.addGestureRecognizer(aLongPress)
16 aLongPress.minimumPressDuration = 1 //需要长按的时间,最小0.5s
17
18 //捏合事件
19 let aPinch = UIPinchGestureRecognizer(target: self, action: "pinchDo:")
20 self.view.addGestureRecognizer(aPinch)
21
22 //旋转事件
23 let aRotation = UIRotationGestureRecognizer(target: self, action: "rotatePiece:")
24 self.view.addGestureRecognizer(aRotation)
25
26 //轻扫事件--左轻扫
27 let leftSwipe = UISwipeGestureRecognizer(target: self, action: "leftSwipe:")
28 self.view.addGestureRecognizer(leftSwipe)
29 leftSwipe.direction = UISwipeGestureRecognizerDirection.Left
30
31 //轻扫事件--右轻扫
32 let rightSwipe = UISwipeGestureRecognizer(target: self, action: "rightSwipe:")
33 self.view.addGestureRecognizer(rightSwipe)
34 rightSwipe.direction = UISwipeGestureRecognizerDirection.Right
35
36 //轻扫事件--上轻扫
37 let upSwipe = UISwipeGestureRecognizer(target: self, action: "upSwipe:")
38 self.view.addGestureRecognizer(upSwipe)
39 upSwipe.direction = UISwipeGestureRecognizerDirection.Up
40
41 //轻扫事件--下轻扫
42 let downSwipe = UISwipeGestureRecognizer(target: self, action: "downSwipe:")
43 self.view.addGestureRecognizer(downSwipe)
44 downSwipe.direction = UISwipeGestureRecognizerDirection.Down
1 //手势
2
3 //点击事件
4 func tapDo(sender:UITapGestureRecognizer)
5 {
6
7 print("点击事件")
8 }
9
10 //拖动事件
11 func handlenPan(sender:UIPanGestureRecognizer)
12 {
13 print("拖动事件")
14
15 if sender.state == .Began
16 {
17 //拖动开始
18 }
19 else if sender.state == .Changed
20 {
21 //拖动过程
22 }
23 else if sender.state == .Ended
24 {
25 //拖动结束
26 }
27 }
28
29 //长摁事件
30 func longPress(sender:UILongPressGestureRecognizer)
31 {
32 print("长摁事件")
33
34
35 }
36
37 //捏合事件
38 func pinchDo(sender:UIPinchGestureRecognizer)
39 {
40 print("捏合")
41 }
42
43 //旋转事件
44 func rotatePiece(sender:UIRotationGestureRecognizer)
45 {
46 print("旋转")
47 }
48
49
50 //轻扫事件--左轻扫
51 func leftSwipe(sender:UISwipeGestureRecognizer)
52 {
53 print("左轻扫")
54 }
55
56 //轻扫事件--右轻扫
57 func rightSwipe(sender:UISwipeGestureRecognizer)
58 {
59 print("右轻扫")
60 }
61
62 //轻扫事件--上轻扫
63 func upSwipe(sender:UISwipeGestureRecognizer)
64 {
65 print("上轻扫")
66 }
67
68 //轻扫事件--下轻扫
69 func downSwipe(sender:UISwipeGestureRecognizer)
70 {
71 print("下轻扫")
72 }
73