Detectar Tap & amp; Mantener en celdas UITableView
-
06-07-2019 - |
Pregunta
¿Cómo detectamos un toque & amp; espera un UITableViewCell
?
Solución
En iOS 3.2 o posterior puede usar UILongPressGestureRecognizer
Otros consejos
Aquí está el código levantado directamente de mi aplicación. Debe agregar estos métodos (y un miembro booleano _cancelTouches) a una clase que deriva de UITableViewCell.
-(void) tapNHoldFired {
self->_cancelTouches = YES;
// DO WHATEVER YOU LIKE HERE!!!
}
-(void) cancelTapNHold {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tapNHoldFired) object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self->_cancelTouches = NO;
[super touchesBegan:touches withEvent:event];
[self performSelector:@selector(tapNHoldFired) withObject:nil afterDelay:.7];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
if (self->_cancelTouches)
return;
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self cancelTapNHold];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self cancelTapNHold];
[super touchesCancelled:touches withEvent:event];
}
//Add gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):
// Add long tap for the main tiles
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
[tile addGestureRecognizer:longPressGesture];
[longPressGesture release];
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(@"gestureRecognizer= %@",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"longTap began");
}
}
Probablemente debería manejar el evento UIControlTouchDown y, dependiendo de lo que quiera decir con "mantener", dispare un NSTimer que contará un intervalo desde que inició el toque e invalida al disparar o soltar el toque. ( UIControlTouchUpInside y eventos UIControlTouchUpOutside ). Cuando se dispara el temporizador, tiene su " toque & amp; mantener " detectado.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow