3

アプリケーション デリゲートは UIResponder サブクラスであるため (Xcode 4.2 以降)、タッチおよびモーション イベントを受信できる必要があります。これを AppDelegate クラスに追加しましたが、機能していません:

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"shake");
    }
}

もちろんView Controllerでも動作しますが、アプリケーション全体の揺れを検出したいです。

4

3 に答える 3

3

これを行う:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self becomeFirstResponder];

}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    NSLog(@"motionBegan");

}

-(BOOL)canBecomeFirstResponder {
return YES;
}
于 2013-08-01T00:15:52.550 に答える
2

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]));
}

これはすべての場合に機能します。

于 2015-03-05T09:11:49.513 に答える
0

私の知る限り、イベントを受信するための最初のレスポンダーになる必要があります。アプリに UINavigationController または UITabController がある場合は、そこで受信を試みることができます。

于 2013-01-23T16:51:12.970 に答える