Lottie-ios: Animation does not play again

Created on 24 Jul 2019  路  10Comments  路  Source: airbnb/lottie-ios

Check these before submitting:

  • [] The issue doesn't involve an Unsupported Feature
  • [X] This issue isn't related to another open issue

This issue is a:

  • [X] Non-Crashing Bug (Visual or otherwise)
  • [] Crashing Bug
  • [] Feature Request
  • [] Regression (Something that once worked, but doesn't work anymore)

Which Version of Lottie are you using?

Lottie 3.0

What Platform are you on?

  • [] MacOS
  • [X] iOS

What Language are you in?

  • [X] Swift
  • [] Objective-C

Expected Behavior


When i scroll back and forth I need animation starts once again.

Actual Behavior


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

Code Example

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
})

Animation JSON

bug

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:

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()
}   
}

All 10 comments

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() 馃槈

Was this page helpful?
0 / 5 - 0 ratings