[英]Long press gesture on UITableView
我一直在嘗試在uitableview上設置長按手勢識別器。 我已經能夠注冊它,但是似乎為我提供了有關該行的錯誤信息。 我的tableview可以正常單擊正常工作,它通過indexPath.row
並且我能夠從與該行相關聯的people
數組中獲取正確的記錄。 但是,在使用下面的代碼時,indexPath.row似乎不一致,並選擇上下行,然后滾動時長按它會選擇隨機記錄。
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
let person = people[indexPath.row]
print("\(person.name)")
///////works but erratic responses//////////
}
}
}
//// in view did load i have this
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(PeopleVC.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
更改此:
let touchPoint = longPressGestureRecognizer.location(in: self.view)
對此:
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
您正在UITableView
而不是UIView
尋找手勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.