Rilevamento Tap & amp; Conserva nelle celle UITableView
-
06-07-2019 - |
Domanda
Come rileviamo un tap & amp; tenere premuto un UITableViewCell
?
Soluzione
In iOS 3.2 o versioni successive puoi utilizzare UILongPressGestureRecognizer
Altri suggerimenti
Ecco il codice estratto direttamente dalla mia app. È necessario aggiungere questi metodi (e un membro booleano _cancelTouches) a una classe derivata da 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");
}
}
Probabilmente dovresti gestire l'evento UIControlTouchDown e, a seconda di cosa intendi con "tieni premuto", attiva un NSTimer che conteggerà un intervallo da quando hai iniziato il tocco e invalidi dopo l'attivazione o il rilascio del tocco (eventi UIControlTouchUpInside e UIControlTouchUpOutside ). Quando il timer scatta, hai il " tocca & amp; contenere " rilevato.