iOS: Comment implémenter un événement "Shake Device"?

Je veux un "Shake Device" événement à mon application – à savoir, quand l'user secoue l'appareil quelque chose se passe. J'ai essayé d'implémenter:

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.subtype == UIEventSubtypeMotionShake) { //something happens } } 

Cela ne semble pas fonctionner …….
Est-ce que quelqu'un sait quelle méthode je devrais utiliser?

Essayez d'utiliser le code ci-dessous, cela a bien fonctionné pour moi.

 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ( event.subtype == UIEventSubtypeMotionShake ) { //your code } if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] ) [super motionEnded:motion withEvent:event]; } - (BOOL)canBecomeFirstResponder { return YES; } 

Peut-être trop tard pour répondre, mais dans votre vueDidLoad vous devez inclure.

 [self becomeFirstResponder]; 

Essayez ça.

En plus de la solution de Taylor, assurez-vous également que votre AppDelegate.m a cela dedans.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { application.applicationSupportsShakeToEdit = YES; return YES; }