[英]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.