2011-01-27 15 views

risposta

27

UILongPressGestureRecognizer è quello che ti serve. Ad esempio,

UILongPressGestureRecognizer *longPress_gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doAction:)]; 
[longPress_gr setMinimumPressDuration:2]; // triggers the action after 2 seconds of press 
[yourButton addGestureRecognizer:longPress_gr]; 

Per lasciare il tuo azione Rimanere attivato solo una volta (cioè., Quando la durata di 2 secondi è finita), assicuratevi di avere il vostro metodo di doAction: simile a questa,

- (void)doAction:(UILongPressGestureRecognizer *)recognizer { 

    if (recognizer.state == UIGestureRecognizerStateBegan) { 

     // Your code here 
    } 
} 
+1

Ho provato il codice, funziona correttamente. Ma c'è un problema che la mia azione innesca due volte. Prima azione innescare dopo 2 secondi e secondi quando termino i tocchi. – Siddiqui

+0

@Arman: Oh .. Ho modificato la mia risposta. Immagino che dovrebbe funzionare. controlla. – EmptyStack

+1

Grazie Simon. È davvero utile per me. – Siddiqui

2

Nell'altro modo è possibile utilizzare this NBTouchAndHoldButton. Questo è esattamente quello che vuoi, ed è molto facile da implementare:

TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom]; 
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2]; 

Buona fortuna!

Problemi correlati