簡體   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