Lottie 3.0
When i scroll back and forth I need animation starts once again.
I have 4 different controllers and every controller has own animation view. Animation is playing only once when I scroll back and forth. After that I have to remove animation view and add animation view with data source again. Without this operation animation won't playing anymore. Please help. How can I start animation from the beginning when I scroll to controller once again.
Here is source code
let animation = Animation.named(self.viewModel?.animationName ?? "")
self.animationView = AnimationView(animation: animation)
self.animationView?.contentMode = .scaleAspectFill
self.animationScene.addSubview(self.animationView ?? UIView())
self.animationView?.frame = self.animationScene.bounds
self.animationView?.play(fromProgress: 0.0, toProgress: progress, loopMode: .playOnce, completion: { (completeAnimation) in
// some code
})
We have this issue too with version 3.1.2 and 3.0.7, but not with version 3.0.6.
We found that dispatching the play() call to the next run loop is an acceptable workaround for us:
func playAnimation() {
DispatchQueue.main.async {
self.animationView.currentProgress = 0
self.animationView.play()
}
}
I was having this issue occurring when entering the viewController for the first time. I would load the AnimationView and then use the play() method on viewDidAppear and nothing would happen. Then If I pushed to another VC and then popped back, the animation would play. My guess is that somehow my JSON file was still being loaded by the AnimationView init when I called the play() method thus, not playing on the first attempt.
I was able to fix this by calling the play() method within the closure of the AnimationView init, so it would start playing once the view was loaded completely. Example below, might be worth a try:
public class LottieTest: UIViewController {
var lottie = AnimationView()
override func viewDidLoad() {
super.viewDidLoad()
self.lottie = AnimationView(url: "lottie URL", closure: { [weak self] _ in
guard let self = self else { return }
self.lottie.play()
})
self.lottie.contentMode = .scaleAspectFill
self.lottie.loopMode = .loop
self.view.addSubview(self.lottie)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.lottie.play()
}
}
Very interesting.
I think it would be reasonable to expect that play() would play as soon as possible...
@buba447 Any inputs? I never had this issue with Lottie 2.5.0 but I had to update it to 3.0+ and suddenly some of my animations were not loading properly, only after the second time on screen.
This seems to be a recurring issue, theres at least 4 issues opened at the moment all related to this. Thanks
Adding to my todo.
going back to 2.5.3 didn't solve problem, i tested on iOS 13 and iOS 12... nothing changed...
Does anyone have a test project which reproduces the issue?
i have the same issue
let provider = BundleImageProvider.init(bundle: Bundle.init(for: self.classForCoder), searchPath: "/resource/images")
let animation = Animation.filepath(filepath, animationCache: nil)
lottoEffectView.imageProvider = provider
lottoEffectView.animation = animation
lottoEffectView.loopMode = .autoReverse
lottoEffectView.setupLoopObservers()
self.lottoanimationView.addSubview(lottoEffectView)
self.lottoEffectView.snp.makeConstraints { (maker) in
maker.left.right.top.bottom.equalToSuperview()
}
self.lottoEffectView.play()
the code is work peferctly , but that i leave the viewcontroller or into background and back this, loop is not work , animation is paused
loopMode must be after play() 馃槈
Most helpful comment
I was having this issue occurring when entering the viewController for the first time. I would load the AnimationView and then use the play() method on viewDidAppear and nothing would happen. Then If I pushed to another VC and then popped back, the animation would play. My guess is that somehow my JSON file was still being loaded by the AnimationView init when I called the play() method thus, not playing on the first attempt.
I was able to fix this by calling the play() method within the closure of the AnimationView init, so it would start playing once the view was loaded completely. Example below, might be worth a try: