我正在开发基于精灵套件的应用。我知道当应用程序处于后台时,所有操作都会暂停。然而,我正在做褪色的事情,所以我想计算应用程序在后台的时间,并使SKAction即褪色动作跳跃一段时间,以便看起来颜色不会改变而应用程序在后台。那么有人可以给我一些关于如何做的详细演练吗?非常感谢!!
答案 0 :(得分:0)
在rmaddy的建议之后编辑。
在应用程序变为活动状态时打开的View Controller中,您可以直接订阅UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification
以在应用程序进入或退出前台时收到通知。
@property(nonatomic) NSDate *enterBackground;
@property (nonatomic) NSTimeInterval enterForeground;
-(void)viewDidLoad{
[super viewDidLoad];
[self subscribeToNotifications];
}
-(void)viewWillDisappear:(BOOL)animated{
[self unsubscribeFromNotifications];
}
-(void)subscribeToNotifications{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEntersBackground) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEntersForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)unsubscribeFromNotifications{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
现在您的ViewController知道应用程序何时进入和退出前台,添加启动和停止计时器的方法。
-(void)appEntersBackground{
self. enterBackground = [NSDate date];
}
-(void)appEntersForeground{
self.enterForeground = [[NSDate date]timeIntervalSinceDate:self.enterBackground];
NSLog(@"%f",self.enterForeground);
}
可以通过self.enterForeground