iOS设备是一个多点触控的设备,在屏幕上可以进行多个手指的控制。那么如何在开发中获取用户的手势操作呢?iOS有四种手指的操作,分别是按下,抬起,移动和取消。四个方法如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//按下屏幕,开始触摸;
override func touchesBegan(touches: Set<nsobject>, withEvent event: UIEvent) {
println(touchesBegan)
}
//手指抬起,结束触摸;
override func touchesEnded(touches: Set<nsobject>, withEvent event: UIEvent) {
println(touchesEnded)
}
//手指移动;
override func touchesMoved(touches: Set<nsobject>, withEvent event: UIEvent) {
println(touchesMoved)
}
//触摸取消
override func touchesCancelled(touches: Set<nsobject>!, withEvent event: UIEvent!) {
println(touchesCancelled)
}</nsobject></nsobject></nsobject></nsobject>
然后通过在控制台打印的方式来测试触摸操作:
(1)手指按下:touchesBegan
(2)手指按下,抬起:touchesBegan-->touchesEnded
(3)手指滑动:touchesBegan-->touchesMoved-->touchesEnded
在测试过程中,并没有发现调用touchesCancelled方法,目前我也不清楚这个方法在何时调用。以后用到了给大家补上。以后可以根据用户的不同操作来进行不同的响应。