Pergunta

Como podemos detectar um toque e segure em um UITableViewCell?

Foi útil?

Solução

Em iOS 3.2 ou posterior você pode usar UILongPressGestureRecognizer

Outras dicas

Aqui está o código levantado em linha reta do meu aplicativo. Você deve adicionar esses métodos (e um membro _cancelTouches boolean) para uma classe você derivar 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");

    }

}

Você provavelmente deve lidar com o UIControlTouchDown evento e dependendo do que você quer dizer com "hold", fogo um NSTimer que contará um intervalo desde que iniciou o toque e invalidar em cima disparando ou liberar o toque ( UIControlTouchUpInside e UIControlTouchUpOutside eventos). Quando os fogos temporizador, você tem o seu "tocar & manter" detectado.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top