Comment détecter un tap & sur un UITableViewCell
?Détection du tap & hold dans les cellules UITableView
Répondre
Dans iOS 3.2 ou une version ultérieure, vous pouvez utiliser UILongPressGestureRecognizer
Vous devriez probablement gérer la UIControlTouchDown événement et en fonction de ce que vous entendez par « hold », tirer un NSTimer qui comptera un intervalle sont écoulées depuis le contact et invalider lors de la cuisson ou de libérer le contact (UIControlTouchUpInside et UIControlTouchUpOutside événements). Lorsque la minuterie se déclenche, vous avez détecté votre "prise &".
Voici le code extrait directement de mon application. Vous devez ajouter ces méthodes (et un membre _cancelTouches booléen) à 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];
}
Vous ne devez jamais utiliser un code comme celui-ci self -> _ cancelTouches = YES; Au lieu d'utiliser simplement self.cancelTouches = YES; et déclarer la propriété privée – Igor
Quelle est cette syntaxe "-> _"? Je ne l'ai jamais vu avant :) –
//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");
}
}
Je suis peut de ne pas assez expert pour venir de cette réponse au code réel ... Mais je veux dire en maintenant enfoncé le même comportement dans Safari mobile lorsque vous tapez et maintenez une URL pour afficher une feuille d'action pour afficher les options concernant cette URL – JFMartin