2009-10-27 19 views

Respuesta

0

Probablemente debería manejar la UIControlTouchDown caso y en función de lo que entendemos por "mantener", disparar un NSTimer que contará un intervalo desde que inició el contacto e invalidar el momento del disparo o liberar al tacto (UIControlTouchUpInside y UIControlTouchUpOutside eventos). Cuando el temporizador se dispara, tiene su "toque & espera" detectado.

+0

estoy lata de no lo suficientemente experto para venir de esta respuesta al código real ... Pero quiero decir con sostenga el mismo comportamiento en Mobile Safari al tocar y mantener una URL para que aparezca una hoja de acción para mostrar las opciones con respecto a esta URL – JFMartin

6

Aquí está el código levantado directamente de mi aplicación. Debe agregar estos métodos (y un miembro _cancelTouches booleano) a una clase derivada 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]; 
} 
+6

Nunca debe usar un código como este self -> _ cancelTouches = YES; En lugar de solo usar self.cancelTouches = YES; y declarar propiedad privada – Igor

+2

¿Qué es esta sintaxis "-> _"? nunca lo había visto antes :) –

6
//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"); 

    } 

} 
Cuestiones relacionadas