UIGestureRecognizerDelegate A set of methods implemented by the delegate of a gesture recognizer to fine-tune an app’s gesture-recognition behavior. 一 ...
UIGestureRecognizerDelegate
A set of methods implemented by the delegate of a gesture recognizer to fine-tune an app’s gesture-recognition behavior.
一個設置手勢識別器委托實現的方法,用於微調應用程式的手勢識別行為。
Overview
The delegates receive messages from a gesture recognizer, and their responses to these messages enable them to affect the operation of the gesture recognizer or to specify a relationship between it and another gesture recognizer, such as allowing simultaneous recognition or setting up a dynamic failure requirement.
這些委托從手勢識別器接收消息,它們會響應這些消息以能夠影響到手勢識別器的操作或指定它與另外一個手勢識別器的關係,例如,允許同時識別或設置動態的失敗需求。
An example of a situation where dynamic failure requirements are useful is in an app that attaches a screen-edge pan gesture recognizer to a view. In this case, you might want all other relevant gesture recognizers associated with that view's subtree to require the screen-edge gesture recognizer to fail so you can prevent any graphical glitches that might occur when the other recognizers get canceled after starting the recognition process. To do this, you could use code similar to the following:
在應用程式中動態的失敗需求例子,應用程式將屏幕邊緣的pan手勢綁定到一個視圖上,在這種情況下,您可能希望與該視圖的子樹關聯的所有其他相關手勢識別器都要求屏幕邊緣手勢識別器失敗,這樣就可以防止在啟動識別過程後取消其他識別器時可能出現的任何圖形錯誤。如果想做到這一點,你可以使用類似於下麵的代碼:
let myScreenEdgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action:#selector(handleScreenEdgePan)) myScreenEdgePanGestureRecognizer.delegate = self // Configure the gesture recognizer and attach it to the view. ... func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool { guard let myView = myScreenEdgePanGestureRecognizer.view, let otherView = otherGestureRecognizer.view else { return false } return gestureRecognizer == myScreenEdgePanGestureRecognizer && otherView.isDescendant(of: myView)}
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.
詢問委托代理是否允許兩個手勢識別器同時識別手勢。
optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
Return Value
true
to allow both gestureRecognizer
and otherGestureRecognizer
to recognize their gestures simultaneously. The default implementation returns false
—no two gestures can be recognized simultaneously.
如果為真的時候允許手勢識別器與其他手勢識別器同時識別他們的手勢。預設實現返回false,不能同時識別兩個手勢。
Discussion
This method is called when recognition of a gesture by either gestureRecognizer
or otherGestureRecognizer
would block the other gesture recognizer from recognizing its gesture. Note that returning true
is guaranteed to allow simultaneous recognition; returning false
, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return true
.
當手勢識別器或其他手勢識別器對某個手勢的識別會阻止其他手勢識別器識別其手勢時,就會調用此方法。註意,返回true保證允許同時識別;另一方面,返回false不能保證防止同時識別,因為其他手勢識別器的委托可能返回true。