【问题标题】:Swift UITapRecognizer steals the tap from an UITableViewSwift UITapRecognizer 从 UITableView 窃取水龙头
【发布时间】:2019-07-26 01:21:55
【问题描述】:

如果我使用 UITapGestureRecognizer,我的 UITableView 对象不会被点击,但识别器运行良好。

点击识别器初始化:

let tap = UITapGestureRecognizer(target: self, action: #selector(tapBackground)) 
menuBackground?.addGestureRecognizer(tap)

Windows 层次结构:

menuBackground?.addSubview(menuWindow!)
menuWindow?.addSubview(menuList!)

效果很好:

@objc func tapBackground(sender: UITapGestureRecognizer) {

  let tapLocation = sender.location(in: menuBackground)
  if !menuWindow!.frame.contains(tapLocation) {
      closeMenu()
  }
}

如果我不使用点击识别器,它工作得很好,但如果我使用它,这个函数什么都不做。为什么?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print( indexPath.row )
 }

【问题讨论】:

    标签: swift uitableview uitapgesturerecognizer


    【解决方案1】:

    添加手势识别器委托,并且您应该在 cellForRowAt 中添加您的 UItapGestureRecognizer。

     func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    

    【讨论】:

    • 我终于找到了解决方案。 tap.cancelsTouchesInView = false
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2021-12-14
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多