2009-07-23 10 views

Respuesta

36

Las API que buscas están en UIResponder:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event; 
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event; 
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event; 

general que acaba de poner en práctica lo siguiente:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (event.type == UIEventSubtypeMotionShake) { 
    //Your code here 
    } 
} 

en la subclase UIViewController (UIViewController es una subclase de UIResponder). Además, desea manejarlo en movimientoEnded: withEvent :, no motionBegan: withEvent :. motionBegan: withEvent: se invoca cuando el teléfono sospecha que está ocurriendo una sacudida, pero el sistema operativo puede determinar la diferencia entre un usuario que tiembla deliberadamente y un temblor accidental (como subir las escaleras). Si el SO decide que no fue una verdadera sacudida después de que motionBegan: withEvent: se invoque, invocará a motionCancelled: en lugar de motionEnded: withEvent :.

+0

Estoy en lo cierto si digo que tenemos que añadir el código de '[auto becomeFirstResponder];' en la vista se requiere agitar el gesto de ¿trabajo? –

+1

Tendrás razón al decir eso. También: (BOOL) canBecomeFirstResponder {return SÍ;} – akaru

3

Joe Hewitt recientemente committed algún código para Three20 que utiliza el evento de agitación 3.0. Parece que solo necesita implementar un código simple dentro de -motionBegan:withEvent: dentro de su UIResponder.

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { 
    if (event.type == UIEventSubtypeMotionShake) { 
     ... 
    } 
} 
7

he publicado un completo ejemplo 3.0 en este hilo:

How do I detect when someone shakes an iPhone?

+0

Gracias, lo leí. Pero este enfoque no usa nueva API. – sashaeve

+0

Sigue leyendo, mi respuesta a esa publicación no utiliza 3.0. Utilizo los eventos publicados anteriormente, solo también explico el poco sobre la necesidad de configurar a UIView como primera respuesta, lo cual es crucial para que realmente funcione. –

Cuestiones relacionadas