iOS:如何检测摇动?

我将下面的代码添加到我的appDelegate.m

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake ) { // User was shaking the device. Post a notification named "shake". [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self]; } } - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { } - (void)shakeSuccess { // do something... } 

然后我补充说:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // INIT THE BACKGROUND PATH STRING [self refreshFields]; [self.window addSubview:navController.view]; [self.window makeKeyAndVisible]; ***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" object:nil];*** return YES; } 

当我在我的iPhone上启动我的应用程序时,名为“shakeSuccess”的方法不会被调用。 我应该怎么做才能在我的应用程序中实现这个function? 任何想法?

这可能会帮助你…
https://stackoverflow.com/a/2405692/796103

他说,你应该将UIApplicationapplicationSupportsShakeToEditYES 。 并在你的VC中覆盖3个方法:

 -(BOOL)canBecomeFirstResponder { return YES; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [self resignFirstResponder]; [super viewWillDisappear:animated]; } 

其余的代码很好。 ( -motionEnded:withEvent:

你可以做这样的事情…

首先…

在应用程序的代理中设置applicationSupportsShakeToEdit属性:

 - (void)applicationDidFinishLaunching:(UIApplication *)application { application.applicationSupportsShakeToEdit = YES; [window addSubview:viewController.view]; [window makeKeyAndVisible]; } 

其次…

添加/覆盖canBecomeFirstResponder,viewDidAppear:和viewWillDisappear:View Controller中的方法:

 -(BOOL)canBecomeFirstResponder { return YES; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; } - (void)viewWillDisappear:(BOOL)animated { [self resignFirstResponder]; [super viewWillDisappear:animated]; } 

第三…

将motionEnded方法添加到您的View Controller中:

 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { // your code } } 

这应该工作,如果第一个答案没有,这只是很快键入没有testing:)

如果您希望能够检测整个应用程序的摇晃动作,最好的方法是用自定义类来覆盖应用程序的类。

然后在您的自定义应用程序类中实现

 @implementation PSApplication - (void)sendEvent:(UIEvent *)event { if ( event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake ) { [[NSNotificationCenter defaultCenter] postNotificationName:@"shakeNotification" object:nil]; } [super sendEvent:event]; } @end 

我扩展了UIApplication类,并添加了对main:MyApplication.h类的引用

 @interface MyApplication : UIApplication @end 

MyApplication.m

 @implementation MyApplication - (void) sendEvent:(UIEvent *)event { if( event && (event.subtype==UIEventSubtypeMotionShake)) { AppDelegate *objAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [objAppDelegate doWhatEver]; [super sendEvent:event]; } else { [super sendEvent:event]; } } @end 

而main.m的最后一步

 int main(int argc, char *argv[]) { return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class])); } 

这适用于所有情况。