swift--觸摸(UITouch)事件(點擊,移動,抬起)

來源:互聯網
上載者:User

標籤:多點觸摸   any   必須   位置   poi   code   display   ack   bsp   

觸摸事件:

UITouch:一個手機第一次點擊螢幕,會形成一個UITouch對象,知道離開銷毀。表示觸碰。UITouch對象能表明當前手指觸碰的螢幕位置、狀態,狀態分為開始觸碰、移動、離開。

具體方法介紹如下:

1.override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)

通知調用者當有一個或者多個手指觸摸到了視圖或者視窗時觸發次方法,touches是UITouch的集合,通過uito我們可以檢測觸摸事件的屬性,是單擊還是雙擊,還有觸摸的位置等。

2.override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)

告訴接受者一個或者多個手指在視圖或者視窗上觸發移動事件。預設不允許多點觸摸,如果要接受多點觸摸事件必須將UIVIew屬性置為true。

//支援多點觸摸self.view.isMultipleTouchEnabled = true

3.override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)

當一個觸摸事件結束時發出的UITouch執行個體對象

4.override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)

通知接受者當系統發出取消事件的時候(如第記憶體消耗時的警告框)

範例代碼:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {        for touch:AnyObject in touches {            let t:UITouch = touch as! UITouch            //當在螢幕上連續拍動兩下時,背景回複為白色            if t.tapCount == 2            {                self.view.backgroundColor = UIColor.white            }else if t.tapCount == 1            {                self.view.backgroundColor = UIColor.blue            }        }    }
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {        //擷取點擊的座標位置        for touch:AnyObject in touches {            let t:UITouch = touch as! UITouch            print(t.location(in: self.view))        }    }
override func touchesEnded(_ touches: Set<UITouch>, with 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.location(in: self.view)            let secondPoint = second.location(in: self.view)            //計算兩點間的距離            let deltaX = secondPoint.x - firstPoint.x            let deltaY = secondPoint.y - firstPoint.y            let initialDistance = sqrt(deltaX + deltaY * deltaY)            print("兩點間的距離:\(initialDistance)")            //計算兩點間的角度            let height = secondPoint.y - firstPoint.y            let width = firstPoint.x - secondPoint.x            let rads = atan(height/width)            let degrees = 180.0 * Double(rads) / .pi            print("兩點間角度:\(degrees)")        }    }
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {        print("event canceled!")    }

 

swift--觸摸(UITouch)事件(點擊,移動,抬起)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.