Question

Comment détecter un tap & amp; conserver un UITableViewCell ?

Était-ce utile?

La solution

Dans iOS 3.2 ou version ultérieure, vous pouvez utiliser UILongPressGestureRecognizer

Autres conseils

Voici le code extrait directement de mon application. Vous devez ajouter ces méthodes (et un membre boolean _cancelTouches) à une classe dérivée 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");

    }

}

Vous devriez probablement gérer l'événement UIControlTouchDown et selon ce que vous entendez par "tenir", déclenchez un NSTimer qui comptera un intervalle depuis que vous avez initié le contact et l'invalider lors du déclenchement ou de la relâchement du contact. (événements UIControlTouchUpInside et UIControlTouchUpOutside ). Lorsque la minuterie se déclenche, vous avez le bouton "Tapez sur & amp; maintenez " détecté.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top