【iOS翻译】对UIGestureRecognizer多种手势傻傻分不清
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。