UITapGestureRecognizer阻止UITableViewCell didSelectRowAt但不阻止IBAction

时间:2018-06-16 07:49:11

标签: swift uitableview uibutton uitapgesturerecognizer

当用户点击View Controller中的任何UITableViewCell时,View Controller中基本视图的UITapGestureRecognizer会捕获触摸事件。我可以通过向UITapGestureRecognizer实例添加委托并过滤掉UITableViewCell来解决此问题。

  • 使用UITapGestureRecognizer的BaseView
    • UIButton with IBAction
    • 带有didSelectRowAt功能的UITableViewCell

但是,我不明白为什么UITapGestureRecognizer不会影响View Controller的按钮。

我添加了一个与IBAction链接的按钮 @IBAction func buttonAction(_ sender: Any) { print("buttonAction") }

以及使用UITapGestureRecognizer的基本视图

view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(baseViewTapAction)))  

@objc func baseViewTapAction() {
    print("baseViewTapAction")
  }

以及带有UITableViewCell的UITableView

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("UITableViewCell")
  }

结果是: 点击UIButton:

buttonAction

点击baseView:

baseViewTapAction

点击UITableViewCell:

baseViewTapAction

0 个答案:

没有答案
相关问题