我们如何将UITapGestureRecognizer分配给另一个视图?

问题描述 投票:0回答:1

我使用这个库来做showcase。

https:/github.comscihantctshowcase。在显示展示时,它发送了一个视图,并将其抛入targetView.我想将已经添加到我发送的视图中的GestureRecognizer添加到targetView中。我怎样才能做到这一点呢?我试过这样的方法。

public func setup(for view: UIView, offset: CGPoint, margin: CGFloat) {
    guard willShow == true else {return}

    targetView = view
    targetOffset = offset
    targetMargin = margin



    guard let targetView = targetView else {return}

    if let recognizer  = view.gestureRecognizers?.first as? UITapGestureRecognizer{
        targetView.isUserInteractionEnabled = true
        targetView.addGestureRecognizer(recognizer)

    }
    ... 
}

但没有成功。我是不是在技术上犯了错误?这不可能吗?

func creatFabButton() {
    fabButton = KCFloatingActionButton()
    fabButton.buttonColor = Util.getColor(Colors.red1)
    fabButton.plusColor = UIColor.white
    let tap = UITapGestureRecognizer(target: self, action:   #selector(fabPresed(_:)))
    fabButton.addGestureRecognizer(tap)
    view.addSubview(fabButton)
}

@objc func fabPresed(_ sender: UITapGestureRecognizer? = nil) {
    let viewController =    NewClientViewController.newViewController(nil)
    self.present(viewController, animated: true, completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()

    creatFabButton()
}
swift uigesturerecognizer floating-action-button uitapgesturerecognizer
1个回答
0
投票

手势识别器是不可重复使用的。它们被设计成只能连接到一个视图。解决方案是为每个视图创建一个专用的手势识别器。

也请看这篇文章。iOS7:UITapGestureRecognizer重用问题

© www.soinside.com 2019 - 2024. All rights reserved.