2017-02-17 40 views
2

我有一個視圖控制器,我正在使用一個計時器來關閉這個視圖控制器。我在那裏有一些按鈕,但我需要用戶使用某種手勢時,我需要重置該計時器。所以我必須處理特定視圖控制器中的每個手勢並在那裏調用重置定時器功能。我知道有一種方法可以覆蓋UIApplication方法(void)sendEvent:併發送一些廣播通知。但我想在視圖控制器上處理它。以某種方式捕獲所有通知和調用方法。如何找出用戶是否在視圖控制器上進行交互?

非常感謝。

回答

0

的UIViewController有一個像

- touchesBegan(_:with:) 
- touchesEnded(_:with:) 
- touchesMoved(_:with:) 
- touchesCancelled(_:with:) 

的方法將是足夠您的需求?

+0

這不是UIViewController's的方法這是UIView的方法 –

0

有兩種方式來處理視圖上的用戶交互/操作。

雙擊手勢:通過使用UITapGestureRecognizer
UITapGestureRecognizer是尋找單個或多個抽頭UIGestureRecognizer的具體子類。對於要識別的手勢,指定數量的手指必須敲擊指定次數的視圖。

func handleTap(sender: UITapGestureRecognizer) { 
    if sender.state == .Ended { 
     // handling code 
    } 
} 


觸:通過使用UITouch(通過從的UIViewController繼承UIResponder
UITouch對象表示的位置,大小,運動,並在屏幕上發生的觸摸的力。

func touchesBegan(Set<UITouch>, with: UIEvent?) // Tells this object that one or more new touches occurred in a view or window. 
func touchesMoved(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more touches associated with an event changed. 
func touchesEnded(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more fingers are raised from a view or window. 
func touchesCancelled(Set<UITouch>, with: UIEvent?) // Tells the responder when a system event (such as a system alert) cancels a touch sequence. 
3

UIViewController繼承自UIResponder類。

因此,您可以選擇實施UIResponder類的所有觸摸事件。

Docs:

func touchesBegan(Set<UITouch>, with: UIEvent?) // Tells this object that one or more new touches occurred in a view or window. 
func touchesMoved(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more touches associated with an event changed. 
func touchesEnded(Set<UITouch>, with: UIEvent?) // Tells the responder when one or more fingers are raised from a view or window. 
func touchesCancelled(Set<UITouch>, with: UIEvent?) // Tells the responder when a system event (such as a system alert) cancels a touch sequence. 

希望這有助於。

+1

是我的錯誤你提到UIResponder :) –

+0

謝謝你的迴應,問題是我有一些按鈕在我的視圖控制器,導航控制器嵌入,搜索。 。當我點擊視圖時觸摸開始工作,沒有按鈕或導航欄。我需要以某種方式覆蓋整個窗口。 –

+0

爲此,您可以在當前視圖頂部添加一個透明視圖(其中包含按鈕和其他控件),然後您可以爲新添加的透明視圖實現相同的委託方法。 –

相關問題