Swift - 触摸事件(点击,移动,抬起等)说明及用例
生活随笔
收集整理的這篇文章主要介紹了
Swift - 触摸事件(点击,移动,抬起等)说明及用例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在iOS開發中,UIGestureRecognizer可以方便的響應處理手勢事件。
而如果要想更精細的處理,我們還需要借助touchesBegan,touchesMoved,touchesEnded等觸摸方法。這些方法 都是UIResponder中的方法。視圖控制器和視圖類,都是UIResponder的子類。正是這個類,讓UIView等相關觸摸事件得以響應。具體方法介紹如下: 1,func touchesBegan(touches: NSSet, withEvent event: UIEvent) 通知調用者當有一個或者多個手指觸摸到了視圖或者窗口時觸發此方法。 touches是UITouch的集合,通過UITouch我們可以檢測觸摸事件的屬性,是單拍還是雙拍,還有觸摸的位置等。
2,func touchesMoved(touches: NSSet, withEvent event: UIEvent) 告訴接收者一個或者多個手指在視圖或者窗口上觸發移動事件。 默認不允許多點觸摸。如果要接收多點觸摸事件必須將UIView的屬性設置為true。
3,func touchesEnded(touches: NSSet, withEvent event: UIEvent) 當一個觸摸事件結束時發出的UITouch實例對象。
4,func touchesCancelled(touches: NSSet, withEvent event: UIEvent) 通知接收者當系統發出取消事件的時候(如低內存消耗的告警框)
下面通過一個樣例演示觸摸事件得用法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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!") ????} } |
轉載于:https://www.cnblogs.com/Free-Thinker/p/4838421.html
總結
以上是生活随笔為你收集整理的Swift - 触摸事件(点击,移动,抬起等)说明及用例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发一则自己创作的Lae程序员小漫画,仅供
- 下一篇: 【转】Linux ln(link) 命令