Các mã sau hoạt động tốt đối với tôi:
Thêm một báo dài cử chỉ recognizer trong viewDidLoad:
// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)
Sau đó, phương pháp được gọi bởi báo chí dài trông giống như sau:
//Called, when long press occurred
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.view)
if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
Ông đã đạt được các chức năng và phải đối mặt với vấn đề với một vòi thêm. –
Tôi không chắc chắn, nếu tôi hiểu vấn đề đúng cách. Những gì tôi có thể nói ít nhất là, rằng với mã của tôi tôi làm một báo dài và tôi nhận được indexPath của hàng mà tôi đã làm báo chí dài. Không cần nhấn thêm. – user3687284
'let touchPoint = longPressGestureRecognizer.locationInView (self.tableView)' là đúng thay vì 'let touchPoint = longPressGestureRecognizer.locationInView (self.view)'. Nếu không, bạn không tính đến rằng 'self.tableView.frame.origin' không được bằng với' CGPointZero' –