skip to Main Content

I use the Lottie animation library in XCode. But when I go to another page, the animation is running in the background. When I go back the main page, the animation is running twice and there is too much RAM being consumed. How can I fix this problem? and how can I stop the animation after a specific time?

import UIKit
import Lottie

class CoffeeSpecialFortunePageVC: UIViewController {
    
    @IBAction func PopUpIn(_ sender: Any) {
        animateIn(desiredView: PopupBlur)
        animateIn(desiredView: popupView)
        animation()
    }
    @IBAction func SuccesBtn(_ sender: UIView) {
        animateOut(desiredView: PopupBlur)
        animateOut(desiredView: popupView)         
    }
         
    @IBOutlet var popupView: AnimationView!
    @IBOutlet var PopupBlur: UIVisualEffectView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.purple
        PopupBlur.bounds = self.view.bounds
        popupView.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width * 0.9, height: self.view.bounds.height * 0.4)
        // Do any additional setup after loading the view.
    }
    func animation() {
        popupView! = .init(name: "check1")
        popupView?.frame = view.bounds
        view.addSubview(popupView!)
        popupView?.play()
        popupView.contentMode = .scaleAspectFit
        popupView.frame.size.height = 350
        popupView.frame.size.width = 350
        popupView.center.x = view.center.x
        popupView.center.y = view.center.y 

             
    }

    func animateIn(desiredView: UIView) {
        let backgraundview = self.view!
        backgraundview.addSubview(desiredView)
        desiredView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
        desiredView.alpha = 0
        desiredView.center = backgraundview.center
        UIView.animate(withDuration: 0.3, animations:{
            desiredView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            desiredView.alpha = 1
            
        })                       
    }
    func animateOut(desiredView: UIView) {
        UIView.animate(withDuration: 0.3, animations: {
            desiredView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
            desiredView.alpha = 0
        },completion: {_ in
            desiredView.removeFromSuperview()                
            
        })            
    }    
}

enter image description here

2

Answers


  1. You can use one of the play(fromProgress...) API’s that Lottie provides

    animationView.play(
        fromProgress: animationView.currentProgress,
        toProgress: 1,
        loopMode: .playOnce,
        completion: { [weak self] completed in
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                self.stopLoopAndTranstion(to: animation, endIn: image)
            }
        }
    )
    

    This will ensure that your current animation will finish and in completion block you can do your finishing work

    Login or Signup to reply.
  2. One of the best solutions:

    1. Create a notifier when the app goes to the background for pause and when back to the foreground

       override func viewDidLoad() 
       {
       super.viewDidLoad()
      
       let notificationCenter = NotificationCenter.default
       notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)
       notificationCenter.addObserver(self, selector: #selector(appBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
      
       animationView = .init(name: "lang")
       animationView!.frame = CGRect(x: 20, y: 100, width: 50, height: 50)
       animationView!.contentMode = .scaleAspectFit
       animationView!.loopMode = .loop
       animationView!.animationSpeed = 0.5
       view.addSubview(animationView!)
       animationView!.play()
      

      }

    2. This will invoke notification methods when the app moves to background and foreground

       @objc func appMovedToBackground() 
       {
      
          print("App moved to background!")
      
          animationView!.removeFromSuperview()
       }
      
      
       @objc func appBecomeActive() 
       {
      
       print("App become active")
       //animationView!.play()
      
       animationView = .init(name: "lang")
       animationView!.frame = CGRect(x: 20, y: 100, width: 50, height: 50)
       animationView!.contentMode = .scaleAspectFit
       animationView!.loopMode = .loop
       animationView!.animationSpeed = 0.5
       view.addSubview(animationView!)
       animationView!.play()
       }
      

    This will solve your problem……. For sure

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search