24

我将以下代码添加到我的 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”的方法。我应该怎么做才能在我的应用程序中实现这个功能?任何想法?

4

4 回答 4

59

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

他说你应该将UIApplication's设置applicationSupportsShakeToEditYES. 并覆盖 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:)

于 2012-04-14T15:55:29.903 回答
26

你可以做这样的事情......

首先...

在 App 的 Delegate 中设置 applicationSupportsShakeToEdit 属性:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    application.applicationSupportsShakeToEdit = YES;

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

第二...

在视图控制器中添加/覆盖 canBecomeFirstResponder、viewDidAppear: 和 viewWillDisappear: 方法:

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];
   [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
 }

第三...

将 motionEnded 方法添加到您的视图控制器:

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

如果第一个答案没有,那应该可以工作,而且这只是快速输入而不是测试:)

于 2013-04-01T22:46:09.320 回答
14

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

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

@implementation PSApplication

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

    [super sendEvent:event];
}

@end
于 2014-05-21T09:44:49.063 回答
3

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

@interface MyApplication : UIApplication

@end

我的应用程序.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]));
}

这适用于所有情况。

于 2015-03-05T09:15:44.620 回答