【iOS翻译】对UIGestureRecognizer多种手势傻傻分不清

 UIGestureRecognizerDelegate

  A set of methods implemented by the delegate of a gesture recognizer to fine-tune an app’s gesture-recognition behavior.  app

  一个设置手势识别器委托实现的方法,用于微调应用程序的手势识别行为。ui

  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.this

  这些委托从手势识别器接收消息,它们会响应这些消息以可以影响到手势识别器的操做或指定它与另一个手势识别器的关系,例如,容许同时识别或设置动态的失败需求。spa

  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手势绑定到一个视图上,在这种状况下,您可能但愿与该视图的子树关联的全部其余相关手势识别器都要求屏幕边缘手势识别器失败,这样就能够防止在启动识别过程后取消其余识别器时可能出现的任何图形错误。若是想作到这一点,你能够使用相似于下面的代码:code

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.orm

  询问委托代理是否容许两个手势识别器同时识别手势。blog

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.ip

  若是为真的时候容许手势识别器与其余手势识别器同时识别他们的手势。默认实现返回false,不能同时识别两个手势。ci

 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。

相关文章
相关标签/搜索