In iOS development, Uigesturerecognizer can handle gesture events in a convenient response.
And if we want to do a finer deal, we need to use touchesbegan,touchesmoved,touchesended and other touch methods. These methods are all methods in Uiresponder. Both the view controller and the view class are subclasses of the Uiresponder. It is this class that allows related touch events such as UIView to respond.
The specific methods are described as follows:1,func Touchesbegan (touches:nsset, withevent event:uievent)Notifies the caller that this method is triggered when one or more fingers touch the view or window. Touches is a collection of Uitouch, through Uitouch we can detect the properties of touch events, is the single shooting or double shot, as well as the location of the touch.
2,func touchesmoved (touches:nsset, withevent event:uievent)Tells the receiver that one or more fingers trigger a move event on a view or window. Multi-touch is not allowed by default. If you want to receive multi-touch events, you must set the UIView property to True.
3,func touchesended (touches:nsset, withevent event:uievent)The Uitouch instance object emitted when a touch event ends.
4,func touchescancelled (touches:nsset, withevent event:uievent)Notifies the recipient when the system issues a cancellation event (such as a low memory consumption alarm box)
Here's a sample to illustrate the use of touch events:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
import UIKit
class ViewController
:
UIViewController {
override func viewDidLoad() {
super
.viewDidLoad()
//支持多点触摸
self
.view.multipleTouchEnabled =
true
}
override func didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
override func touchesBegan(touches:
Set
<
NSObject
>, withEvent event:
UIEvent
) {
for touch:
AnyObject in touches {
var t:
UITouch = touch
as
!
UITouch
//当在屏幕上连续拍动两下时,背景恢复为白色
if
(t.tapCount == 2)
{
self
.view.backgroundColor =
UIColor
.whiteColor()
}
//当在屏幕上单击时,屏幕变为红色
else if
(t.tapCount == 1)
{
self
.view.backgroundColor =
UIColor
.redColor()
}
println
(
"event begin!"
)
}
}
override func touchesMoved(touches:
Set
<
NSObject
>, withEvent event:
UIEvent
)
{
for touch:
AnyObject in touches {
var t:
UITouch = touch
as
!
UITouch
println
(t.locationInView(
self
.view))
}
}
override func touchesEnded(touches:
Set
<
NSObject
>, withEvent event:
UIEvent
)
{
//两点触摸时,计算两点间的距离
if touches.count == 2{
//获取触摸点
let first = (touches
as NSSet
).allObjects[0]
as
!
UITouch
let second = (touches
as NSSet
).allObjects[1]
as
!
UITouch
//获取触摸点坐标
let firstPoint = first.locationInView(
self
.view)
let secondPoint = second.locationInView(
self
.view)
//计算两点间的距离
let deltaX = secondPoint.x - firstPoint.x
let deltaY = secondPoint.y - firstPoint.y
let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY)
println
(
"两点间距离:\(initialDistance)"
)
}
println
(
"event end!"
)
}
override func touchesCancelled(touches:
Set
<
NSObject
>, withEvent event:
UIEvent
)
{
println
(
"event canceled!"
)
}
}
|
Swift-Touch Events (click, move, lift, etc.) description and use cases