iOS:如何实施“摇动设备”事件?

我想在我的应用程序中使用“摇动设备”事件 – 即,当用户摇动设备时会发生一些事情。 我试着实施:

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

它似乎不起作用…….
有谁知道我应该使用哪种方法?

尝试使用下面的代码,它对我来说很好。

 - (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; } 

可能会迟到回答,但在你的viewDidLoad你需要包括。

 [self becomeFirstResponder]; 

试一试。

除了Taylor的解决方案,还要确保你的AppDelegate.m中有这个。

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