ios - recognizer - Cómo seleccionar una fila de la tabla durante una pulsación larga en Swift
uipangesturerecognizer swift 4 (8)
Tengo una tabla que tiene un reconocedor de gestos de pulsación larga que ejecuta código dependiendo de la fila de la tabla seleccionada.
El problema que tengo es que actualmente tengo que tocar la fila que quiero y luego presionar prolongadamente.
¿Cómo puedo hacer que la tabla seleccione la fila que mantengo presionada sin tener que tocar para seleccionarla primero?
El siguiente código funciona bien para mí:
Agregue un reconocedor de gestos de pulsación larga en viewDidLoad:
// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)
Entonces el método invocado por la pulsación larga se ve así:
//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
}
}
Función Swift 3:
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)
Para evitar esto, puede agregar UILongPressGestureRecognizer
dentro de cellForRowAtIndexPath
lugar de didSelectRowAtIndexPath
Para verision 3 de swift
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
}
}
}
En su función viewDidLoad
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)
Usando IBAction puedes hacer (para un 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)!")
}
}
}
Recuerda enlazar con tu Reconocedor de Gestos de Prensa Larga.
Swift 4
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)
// MARCAR: Acciones
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let touchPoint = sender.location(in: self.view)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print("Long pressed row: /(indexPath.row)")
}
}
}
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) {
}
}
}
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tblMessage.addGestureRecognizer(longPressGesture)
}
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
}
}
}
C objetivo
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];
-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
if (pGesture.state == UIGestureRecognizerStateRecognized)
{
//Do something to tell the user!
}
if (pGesture.state == UIGestureRecognizerStateEnded)
{
UITableView* tableView = (UITableView*)self.view;
CGPoint touchPoint = [pGesture locationInView:self.view];
NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
if (row != nil) {
//Handle the long press on row
}
}
}
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap)))
self.addGestureRecognizer(longPressGesture)
func longTap(){
print("Long tap")
}