use*_*482 0 uigesturerecognizer ios swift
我一直在尝试在我的 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)
Run Code Online (Sandbox Code Playgroud)
改变这个:
let touchPoint = longPressGestureRecognizer.location(in: self.view)
Run Code Online (Sandbox Code Playgroud)
对此:
let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
Run Code Online (Sandbox Code Playgroud)
你在你的UITableView而不是你的UIView.
| 归档时间: |
|
| 查看次数: |
4049 次 |
| 最近记录: |