2015-06-15 12 views
11

Tôi có một bảng có trình nhận dạng cử chỉ báo chí dài chạy mã tùy thuộc vào hàng bảng nào được chọn.Cách chọn hàng của bảng trong một lần bấm dài trong Swift

Sự cố tôi gặp phải là hiện tại tôi phải nhấn vào hàng mà tôi muốn sau đó thực hiện báo chí dài.

Làm cách nào để làm cho bảng chọn hàng mà tôi đang nhấn và không nhấn để chọn đầu tiên?

Trả lời

1

Để tránh điều này, bạn có thể thêm UILongPressGestureRecognizer bên trong cellForRowAtIndexPath thay vì didSelectRowAtIndexPath

21

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 
    } 
} 
+0

Ô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. –

+0

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

+3

'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' –

1

Sử dụng IBAction bạn có thể làm (đối với một CollectionView):

@IBAction func handleLongPress(sender: AnyObject) { 

    if sender.state == UIGestureRecognizerState.Began 
    { 
     let position = sender.locationInView(sender.view) 

     if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{ 
      print("You holding cell #\(indexPath.item)!") 
     } 
    } 
} 

Hãy nhớ liên kết với Press dài của bạn Gesture Recognizer.

14

Swift 3 chức năng:

func handleLongPress(_ 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 
    } 
} 

viewDidLoad:

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:))) 
longPressGesture.minimumPressDuration = 1.0 // 1 second press 
longPressGesture.delegate = self 
self.tableView.addGestureRecognizer(longPressGesture) 

thêm: https://github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md

+0

Nó không phải dành cho 3.0 nhanh như bạn đã đề cập. nó vẫn chỉ nhanh chóng <3.0 – Sahil

+0

Làm việc hoàn hảo trong Swift 3 cho tôi. –

0
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap))) 
self.addGestureRecognizer(longPressGesture) 

func longTap(){ 
    print("Long tap") 
} 
6

Đối Verision 3 nhanh chóng

func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) { 
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began { 
     let touchPoint = longPressGestureRecognizer.location(in: self.view) 
     if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) { 
      print("indexPath=\(indexPath)") 
      // your code here, get the row for the indexPath or do whatever you want 
     } 
    } 
} 

Trong viewDidLoad Chức năng của bạn

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:))) 
    longPressGesture.minimumPressDuration = 1.0 // 1 second press 
    longPressGesture.delegate = self as? UIGestureRecognizerDelegate 
    self.notificationTabelView.addGestureRecognizer(longPressGesture) 
1

Swift 4

override func viewDidLoad() { 
    super.viewDidLoad() 
    setupLongPressGesture() 
} 

func setupLongPressGesture() { 
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress)) 
    longPressGesture.minimumPressDuration = 1.0 // 1 second press 
    longPressGesture.delegate = self 
    self.tblMessage.addGestureRecognizer(longPressGesture) 
} 

@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){ 
    if gestureRecognizer.state == .ended { 
     let touchPoint = gestureRecognizer.location(in: self.tblMessage) 
     if let indexPath = tblMessage.indexPathForRow(at: touchPoint) { 

     } 
    } 
} 
Các vấn đề liên quan