在swift中使用UIPanGestureRecognizer重载gestureRecognizerShouldBegin

时间:2014-11-23 17:38:44

标签: swift overloading uipangesturerecognizer

我有一个平移手势,我需要检查手势何时开始。在objective-C中,您可以使用

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

可以使用UIPanGestureRecognizer重载

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer

但快速

override func gestureRecognizerShouldBegin(gestureRecognizer: UIPanGestureRecognizer) -> Bool {

它不起作用并返回

使用选择器覆盖方法' gestureRecognizerShouldBegin:'具有不兼容的类型'(UIPanGestureRecognizer) - >布尔'

如何在swift中重载此方法?

2 个答案:

答案 0 :(得分:2)

您不需要重载该方法,您可以改为:

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let panGesture = gestureRecognizer as? UIPanGestureRecognizer {
        return ...
    }
    else {
        return ...
    }
}

答案 1 :(得分:0)

删除覆盖,不要忘记添加UIGestureRecognizerDelegate扩展程序。

您可以在课程底部添加

class YourViewController: UIViewController {
    // You class declaration
}

extension YourViewController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //
        return true
    }
}