3

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!

Community
  • 1
  • 1
D. Greg
  • 991
  • 1
  • 9
  • 21
  • 2
    are you reseting the alpha of the board back to 1.0 before calling the animation method? – Blake Lockley Mar 08 '16 at 02:43
  • Yes, it is. It runs the same animation when the view loads originally. That one works fine. – D. Greg Mar 08 '16 at 02:45
  • 1
    sorry, my comment wasn't very clear, I mean, when you first create the view you are trying to animate it has an alpha of 1, and then you animate it the first time o zero, after the first time the animation has run, have you set the alpha of board back to one again? – Blake Lockley Mar 08 '16 at 02:49
  • i didnt see you had autoreverse in the options! my bad, sorry – Blake Lockley Mar 08 '16 at 02:50
  • @BlakeLockley no worries. Yes it has an autoReverse – D. Greg Mar 08 '16 at 03:00
  • @BlakeLockley Thanks for your help. If you Make your comment into an answer, I'll mark it as the answer (or cut and paste mine) – D. Greg Mar 08 '16 at 04:11

1 Answers1

2

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.

D. Greg
  • 991
  • 1
  • 9
  • 21
  • 1
    Im glad i could indirectly help, haha! It must be a matter of swift thinking that the alpha was still set to zero behind the scenes or something, Im gonna have a look see if I can find the reason why. – Blake Lockley Mar 08 '16 at 04:14