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。