2011-01-27 11 views

Respuesta

27

UILongPressGestureRecognizer es lo que necesita. Por ejemplo,

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]; 

dejar que su acción Se activa sólo una vez (es decir., Cuando la duración de 2 segundos ha terminado), asegúrese de que tiene su método de doAction: ve algo como esto,

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

    if (recognizer.state == UIGestureRecognizerStateBegan) { 

     // Your code here 
    } 
} 
+1

He intentado su código, está funcionando bien. Pero hay un problema: mi acción se dispara dos veces. Activación de la primera acción después de 2 segundos y segundos cuando toco el final. – Siddiqui

+0

@Arman: Oh ... he editado mi respuesta. Supongo que debería funcionar. Echale un vistazo. – EmptyStack

+1

Gracias Simon. Es realmente útil para mí – Siddiqui

2

Por otro lado, puede usar this NBTouchAndHoldButton. Esto es exactamente lo que quiere, y es muy fácil de implementar:

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

¡Buena suerte!

Cuestiones relacionadas