Domanda

Come potrei seguire un evento come tocco sullo schermo di iPhone per 2 secondi. Come in Safari per salvare l'immagine per immagine Aggiunta a un UIWebView?

È stato utile?

Soluzione

Crea un NSTimer con +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: nel metodo -touchesBegan:withEvent: di lei, e annullarlo (usando -invalidate) in -touchesEnded:withEvent:. Se il metodo suoi punti selettore a viene chiamato, quindi l'utente tiene il dito sulla vista per qualunque durata impostata intervallo del timer a. Esempio:

Interfaccia (h):

@interface MyView : UIView
    ...
    NSTimer *holdTimer;
@end

attuazione (.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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top