Como rastrear uma espera longa para um dedo no iPhone?
Pergunta
Como eu poderia rastrear um evento como toque na tela do iPhone por 2 segundos. Como no Safari, para salvar a imagem para a imagem adicionada a um UIWebView?
Solução
Criar um nstimer com +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Na sua opinião -touchesBegan:withEvent:
método e cancelá -lo (usando -invalidate
) dentro -touchesEnded:withEvent:
. Se o método que seu seletor aponta for chamado, o usuário segurou o dedo na visualização por qualquer duração para a qual você defina o intervalo do timer. Exemplo:
Interface (.h):
@interface MyView : UIView
...
NSTimer *holdTimer;
@end
Implementação (.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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow