5

アプリ全体にシェイク機能を統合したい。だから私はappDelegateですべてをやっています。viewControllerをプッシュする必要があります。motionBeganをプッシュできますが、motionEndedを実行したかったのです。はいモーション終了はViewControllerで機能しますが、アプリデリゲートでは呼び出されません。として行う

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder{
    return YES;
}

motionEndedは呼び出されません

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

motionBeginが呼び出されました

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(event.subtype==UIEventSubtypeMotionShake){
        NSLog(@"motionBegan called");
    }
}
4

1 に答える 1

1

基本的に、必要に応じてviewControllerforまたは any を登録できますapplicationDidBecomeActiveNotification

たとえば、あなたviewControllerviewDidLoadメソッドでは、通知用に登録できます

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMethod)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];

このメソッドをクラスに実装するmyMethodと、アプリケーションがアクティブになるたびに呼び出されます

-(void) myMethod(){
 // do your stuff
}

最後に、dealloc メソッドの通知から viewController を登録解除します

-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2013-03-05T12:20:36.140 に答える