我想在整个应用程序中整合摇动功能。因此,我正在执行appDelegate中的所有操作。我需要推送一个viewController,我能够推送motionBegan,但是我想这样做motionEnded。是的,运动结束在 View Controller 中确实起作用,但是在应用程序委托(delegate)中没有被调用。
做为

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

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

最佳答案

您基本上可以根据自己的需要注册viewControllerapplicationDidBecomeActiveNotification
例如,在viewControllerviewDidLoad方法中,您可以注册它以进行通知

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

并在您的类(class)中实现此方法,则每次应用程序激活时,您的myMethod都会调用
-(void) myMethod(){
 // do your stuff
}

最终从dealloc方法中的通知中取消注册viewController
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

10-07 19:43
查看更多