gestureRecognizerShouldBegin未被调用

时间:2015-11-18 20:59:34

标签: ios swift uigesturerecognizer

我有一个UIPinchGestureRecognizer,允许用户缩放UITextView。这一切都适用于以下代码:

@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
      view.transform = CGAffineTransformScale(view.transform,
        recognizer.scale, recognizer.scale)

        recognizer.scale = 1
    }
  }

但是,我只希望用户能够使UITextView更大,而不是更小。我已经设置了我的UIGestureRecognizerDelegate并输入了以下代码:

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let pinch = gestureRecognizer as? UIPinchGestureRecognizer {
        return pinch.scale < 1
    }
    return true
}

我从这篇帖子中得到了代码:UIPinchGestureRecognizer only for pinching out

然而,我的方法永远不会被调用,它无法正常工作。有什么想法吗?

1 个答案:

答案 0 :(得分:8)

确保在您的班级声明中包含UIGestureRecognizerDelegate

class ViewController: UIViewController, UIGestureRecognizerDelegate { /* ... */ }

然后,将gestureRecognizer的代理人设置为self

gestureRecognizer.delegate = self

希望有所帮助:)