UIGestureRecognizer 未向其他人发送操作 class

UIGestureRecognizer not sending action to other class

我正在尝试从 UIView class 上的 UIGestureRecognizer 向其父视图控制器发送一个动作。

HomeViewController 包含一个包含多个 (5) OperationView 的视图。

OperationView 扩展了 UIView,看起来像这样:

class OperationView: UIView {
    override func awakeFromNib() {
        super.awakeFromNib()

        // OperationView is on a view that has HomeViewController as it's controller in StoryBoard
        let swipeGestureRecognizer = UISwipeGestureRecognizer(target: HomeViewController.self(), action: #selector(HomeViewController.operatorWasSwiped(_:)))
        swipeGestureRecognizer.direction = .left

        let tapGestureRecognizer = UITapGestureRecognizer(target: HomeViewController.self(), action: #selector(HomeViewController.operatorWasSwiped(_:)))

        self.addGestureRecognizer(swipeGestureRecognizer)
        self.addGestureRecognizer(tapGestureRecognizer)
    }

在 HomeViewController 中,我有一个应该由 UIGestureRecognizers 调用的函数:

@objc func operatorWasSwiped(_ sender : UIGestureRecognizer) {
    if (sender.isKind(of: UITapGestureRecognizer.self)) { print("Tapped") }
    else { print("Swiped") }

    self.performSegue(withIdentifier: "segue1", sender: nil)
}

但是,我什么也没得到。甚至没有错误。

我试过使用 HomeViewController 上的 IBOutlet 将 UIGestureRecognizer 添加到每个 OperationView:

view1.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view2.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view3.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view4.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view5.swipeGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))

view1.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view2.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view3.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view4.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))
view5.tapGestureRecognizer?.addTarget(self, action: #selector(operatorWasSwiped(_ :)))

view1.addGestureRecognizer(view1.swipeGestureRecognizer!)
view2.addGestureRecognizer(view2.swipeGestureRecognizer!)
view3.addGestureRecognizer(view3.swipeGestureRecognizer!)
view4.addGestureRecognizer(view4.swipeGestureRecognizer!)
view.addGestureRecognizer(view5.swipeGestureRecognizer!)

view1.addGestureRecognizer(view1.tapGestureRecognizer!)
view2.addGestureRecognizer(view2.tapGestureRecognizer!)
view3.addGestureRecognizer(view3.tapGestureRecognizer!)
view4.addGestureRecognizer(view4.tapGestureRecognizer!)
view5.addGestureRecognizer(view5.tapGestureRecognizer!)

我添加的地方:

var swipeGestureRecognizer: UISwipeGestureRecognizer?
var tapGestureRecognizer: UITapGestureRecognizer?

OperationViewclass 个变量

这行得通,但远谈不上优雅(想象一下,如果我必须添加另一个视图……有点违背我之前的抽象观点)

知道发生了什么事吗?或者我错过了什么?

我正在使用 Swift 4Xcode 9

您正在方法内部创建该手势,并且该手势可能仅在调用该函数时存在于该范围内。所以技术上在你使用它之前就消失了。因此,在方法之外创建该手势的实例可能会在视图存在时存在。如果说得通。

更新:正如评论所指出的,我们确实需要父级 viewController 的当前引用。

这样做怎么样..

class OperationView: UIView {

let swipeGestureRecognizer = UISwipeGestureRecognizer()
let tapGestureRecognizer = UITapGestureRecognizer()

override func awakeFromNib() {
    super.awakeFromNib()

    if let parentViewController = parentViewController {
    swipeGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))
    swipeGestureRecognizer.direction = .left

    tapGestureRecognizer.addTarget(parentViewController, action: #selector(HomeViewController.operatorWasSwiped(_:)))

    self.addGestureRecognizer(swipeGestureRecognizer)
    self.addGestureRecognizer(tapGestureRecognizer)
    }

   }
}

extension UIView {

var parentViewController: UIViewController? {
    var parentResponder: UIResponder? = self

    while parentResponder != nil {
        parentResponder = parentResponder!.next

        if let viewController = parentResponder as? UIViewController {
            return viewController
        }
    }
    return nil
   }
}