【问题标题】:How to calculate the time in background and make SKAction jump to the time after如何在后台计算时间并使SKAction跳转到之后的时间
【发布时间】:2015-01-17 14:21:59
【问题描述】:

我正在开发一个基于 sprite kit 的应用程序。我知道应用程序在后台时所有操作都会暂停。但是,我正在做褪色的事情,所以我想计算应用程序处于后台时的时间,并使作为褪色动作的 SKAction 在之后跳转时间,这样看起来颜色不会在应用程序在后台。那么有人可以给我一些关于如何做到这一点的详细演练吗?非常感谢!!

【问题讨论】:

  • 如果这只是一个视觉效果,从背景返回时设置一个随机颜色 - 没有人会注意到;)

标签: ios sprite-kit skaction


【解决方案1】:

根据 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访问

【讨论】:

  • 为什么要为此定义自己的通知?摆脱应用程序委托方法中的代码,只需订阅视图控制器中的标准(UIApplicationDidBecomeActiveNotificationUIApplicationWillResignActiveNotification)通知。顺便说一句 - 这些是要使用的常量,而不是字符串文字。
  • @rmaddy 好点子,不知道为什么我把它弄得这么复杂!
  • 非常感谢!!我已经计算了后台的时间。但我对如何在应用程序激活后跳转条件有疑问。我正在做一个关于褪色某物颜色的skaction。你能给我一些建议吗?
  • @zh_Vincent 我不确定你在说什么条件。能不能说的具体点?
  • 你知道如何使用 SKAction 淡化 SKShapeNode 的填充颜色吗?我就是这种情况。因此,在应用程序响应进入前台后,我对如何做到这一点有点迷茫。我想知道之后如何跳转到条件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多