D. Greg
D. Greg

Reputation: 1000

Restart animation when returning to foreground

I'm having an issue restarting an animation when the app returns to the foreground. I checked this solution, but it's not working for me.

In my App Delegate, I'm cancelling the animation when the app resigns

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

And I'm posting a notification to the View Controller when the app enters the foreground

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

The View Controller has an observer that calls the method, but the animation isn't restarting.

Called Method:

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

the boardFlash animation:

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)
}

I checked that everything is getting called, but the animation isn't running. Any help will be greatly appreciated. Thanks!

Upvotes: 4

Views: 1028

Answers (1)

D. Greg
D. Greg

Reputation: 1000

I solved it thanks to @BlakeLockley comment.

I set the board Image View alpha to 1.0 in the observer method call.

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

Now it works fine. Interesting solution though.

Upvotes: 2

Related Questions