繁体   English   中英

UITapGestureRecognizer不适用于自定义UIView类

[英]UITapGestureRecognizer won't work for custom UIView class

我有一个自定义UIView类,它创建一个复选框。 此UIView在自定义表格视图单元格中。

我在cellForRowAt有此代码

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TransactionsTableViewCell
    cell.isUserInteractionEnabled = true
    cell.addSubview(cell.test)
    let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.recurringChange(_:)))
    cell.test.isUserInteractionEnabled = true
    cell.test.addGestureRecognizer(gesture)
  }

我在ViewController类中有此功能

@objc func recurringChange(_ sender: 
UITapGestureRecognizer) {
    print("test")
}

轻按test视图时,它不会打印测试。 我使用普通的UIView(非自定义)进行了尝试,并且效果完全符合预期。

如果这样做有帮助,那么这里是自定义类的链接: https : //github.com/vladislav-k/VKCheckbox

我已经在Xcode 10中测试了代码,没有问题。

您可以在添加TapGesture之前检查“ cell.isUserInteractionEnabled = true”和“ cell.addSubview(test)”是否已添加到单元格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM