Prabhdeep Singh Sodhi
Prabhdeep Singh Sodhi

Reputation: 135

Restarting animations ios swift

The animation stops working after i

  1. Transits to another view controller and comes back
  2. app goes to background and then comes back to foreground

i am using this code (in viewwillappear)to achieve blinking animation

    UIView.animate(withDuration: 1.0, delay: 0.4, options:[ UIViewAnimationOptions.curveEaseOut , .repeat], animations: {

        self.logoLabel1.alpha = 0.0
        self.logoLabel2.alpha = 0.0

    }, completion: nil)

Can anyone help me? thanks.

Upvotes: 1

Views: 482

Answers (1)

Meeran Tariq
Meeran Tariq

Reputation: 1436

Yeah, it is the expected behaviour. Animations will stop when the view disappears, either by minimizing the app or by showing another viewcontroller. Move your animation code to viewDidAppear, and the animation will not stop when you move to any other viewcontroller and come back. For handling the case where animation stops when app goes to background, use following code:

Inside your viewDidAppear,

NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)

and in your viewWillDisappear,

NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)

and write this function inside your viewcontroller,

@objc func willEnterForeground() {
    // your animations
    UIView.animate(withDuration: 1.0, delay: 0.4, options:[ UIViewAnimationOptions.curveEaseOut , .repeat], animations: {

        self.logoLabel1.alpha = 0.0
        self.logoLabel2.alpha = 0.0

    }, completion: nil)
}

Upvotes: 4

Related Questions