返回前台时重新启动动画

Restart animation when returning to foreground

我在应用 returns 到前台时重新启动动画时遇到问题。我检查了这个 solution,但它对我不起作用。

在我的 App Delegate 中,我在应用退出时取消动画

func applicationWillResignActive(application: UIApplication) {
    let rootVC = window?.rootViewController as! ViewController
    rootVC.boardImage.layer.removeAllAnimations()
}

当应用程序进入前台时,我会向视图控制器发送通知

func applicationWillEnterForeground(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName("didEnterForeground", object: nil)
}

View Controller 有一个调用该方法的观察者,但动画没有重新启动。

调用方法:

func playAnimation(notification: AnyObject) {
    gameLogic.boardFlash(boardImage)
}

boardFlash动画:

func boardFlash(board: UIImageView) {
    let options:UIViewAnimationOptions = [.Autoreverse, .Repeat]

    UIView.animateWithDuration(0.3, delay: 1.0, options: [options], animations: {
        board.alpha = 0.0
        }, completion: nil)
}

我检查过所有内容都在调用,但动画没有 ​​运行。任何帮助将不胜感激。谢谢!

感谢@BlakeLockley 的评论,我解决了它。

我在观察者方法调用中将板图像视图 alpha 设置为 1.0。

func playAnimation(notification: AnyObject) {
    boardImage.alpha = 1.0
    gameLogic.boardFlash(boardImage)
}

现在可以正常使用了。有趣的解决方案。