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()
})
}
}
2
Answers
You can use one of the
play(fromProgress...)
API’s thatLottie provides
This will ensure that your current animation will finish and in completion block you can do your finishing work
One of the best solutions:
Create a notifier when the app goes to the background for pause and when back to the foreground
}
This will invoke notification methods when the app moves to background and foreground
This will solve your problem……. For sure