2015-06-15 15 views
11

Mam tabelę z długim rozpoznawaniem gestów, który uruchamia kod w zależności od wybranego wiersza tabeli.Jak wybrać wiersz tabeli podczas długiego naciśnięcia przycisku Swift

Problem polega na tym, że obecnie muszę dotknąć żądanego wiersza, a następnie nacisnąć.

W jaki sposób ustawić stół, aby wybrać wiersz, który długo naciska, bez konieczności stuknięcia, aby go najpierw wybrać?

Odpowiedz

1

Aby tego uniknąć, można dodać UILongPressGestureRecognizer wewnątrz cellForRowAtIndexPath zamiast didSelectRowAtIndexPath

21

Poniższy kod działa dobrze dla mnie:

Dodaj długie naciśnięcie gest rozpoznawania w viewDidLoad:

// tapRecognizer, placed in viewDidLoad 
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:") 
self.view.addGestureRecognizer(longPressRecognizer) 

Następnie metoda wywoływana przez długie naciśnięcie wygląda następująco:

//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

On już osiągnął funkcjonalność i stoi problem z dodatkowym kranu. –

+0

Nie jestem pewien, jeśli rozumiem problem prawidłowo. Co mogę powiedzieć, przynajmniej jest to, że za pomocą mojego kodu robię długie naciśnięcie i otrzymuję indexPath rzędu, w którym zrobiłem długie naciśnięcie. Nie potrzeba dodatkowego kranu. – user3687284

+3

'let touchPoint = longPressGestureRecognizer.locationInView (self.tableView)' jest poprawne zamiast 'let touchPoint = longPressGestureRecognizer.locationInView (self.view)'. W przeciwnym razie nie bierze się pod uwagę, że 'self.tableView.frame.origin' nie może być równe' CGPointZero' –

1

Korzystanie IBAction można zrobić (dla 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)!") 
     } 
    } 
} 

Pamiętaj, aby połączyć się ze swoim długim naciśnięciu Gest Recognizer.

14

Swift 3 Zastosowanie:

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) 

więcej: https://github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md

+0

. Nie dotyczy to Swift 3.0, o którym wspomniałeś. nadal wskazuje na szybkość <3.0 – Sahil

+0

Działa doskonale w Swift 3 dla mnie. –

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

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

Na Verision 3 szybki

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

In Your viewDidLoad funkcji

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) { 

     } 
    } 
} 
Powiązane problemy