0

I have two animation which play on my home screen view controller. They are both animations from LottieFiles. One is an alpha colored changing background and the other is an infinite loop with different colors. When a new modal view over current context is selected I want to pause both animations and start them up again once the modal view is dismissed.

I have tried calling the animationView.pause method however they still continue to play in the background. I would like to pause them for better energy efficiency.

class HomeScreen: UIViewController{
 let animationView = AnimationView()
 let animationTwo = AnimationView()

 func showSupport() {
    
    let modalViewController = SupportViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    present(modalViewController, animated: true, completion: nil)
}
 func showSInfo() {
    
    let modalViewController = InfoViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    present(modalViewController, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
    
    super.viewDidAppear(animated)
    
    
    animationView.play(fromProgress: 0,
                       toProgress: 1,
                       loopMode: LottieLoopMode.loop,
                       completion: { (finished) in
                        if finished {
                            print("Animation Complete")
                        } else {
                            print("Animation cancelled")
                        }
    })
    
    
    animationTwo.play(fromProgress: 0,
                       toProgress: 1,
                       loopMode: LottieLoopMode.loop,
                       completion: { (finished) in
                        if finished {
                            print("Animation Complete")
                        } else {
                            print("Animation cancelled")
                        }
    })
    
}
 override func viewDidLoad() {
   let animation = Animation.named("bg12")
    animationView.animation = animation
    animationView.contentMode = .scaleAspectFill
    view.addSubview(animationView)

    let animationViewTwo = Animation.named("infi2")
    animationTwo.animation = animationViewTwo
    animationTwo.contentMode = .scaleAspectFit
    animationTwo.alpha = 0.7
    view.addSubview(animationTwo)
 }
    }


 class SupportViewController: UIViewController {
     let homeVC = HomeScreen()
    
  override func viewDidLoad() {
    homeVC.animationView.pause()
    homeVC.animationTwo.pause()
    super.viewDidload()
}
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
soRazor
  • 137
  • 9
  • 1
    This will never work, you are creating new `HomeScreen` and this way new animations and you are pausing this new animations – Lu_ Oct 23 '19 at 06:41

1 Answers1

0

Can't you put the pausing to the completion handler of the present, like:

present(modalViewController, animated: true) {
    self.animationView.pause()
}
serzso
  • 49
  • 3