Cómo realizar el seguimiento de una larga espera por un dedo en el iPhone?
Pregunta
¿Cómo iba a realizar un seguimiento de un evento como el tacto en la pantalla de iPhone durante 2 segundos. Al igual que en Safari imagen para ahorrar para una imagen añadida a una UIWebView?
Solución
Crear una NSTimer con +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
en el método -touchesBegan:withEvent:
de su vista, y cancelarla (usando -invalidate
) en -touchesEnded:withEvent:
. Si el método de selección de sus puntos en obtiene la llamada, el usuario tuvo su dedo en la vista por cualquier duración que determine el intervalo del temporizador a. Ejemplo:
Interface (.h):
@interface MyView : UIView
...
NSTimer *holdTimer;
@end
Implementación (.m):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = nil;
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow