lottie-ios: The animation stops when I navigate to other page and go back

Lottie-iOS Issue

I create a custom LottieView like below, it works fine for me with looping. However, when I navigate to other page then go back, the animation stops. I don’t find anyway to restart or resume it. Could someone help me please?

import SwiftUI
import Lottie
//Following this tutorial: https://designcode.io/swiftui-handbook-lottie-animation 
struct LottieView: UIViewRepresentable {
    var name = "success"
    var loopMode: LottieLoopMode = .loop
    
    func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
        let view = UIView(frame: .zero)
        let animationView = AnimationView()
        let animation = Animation.named(name)
        animationView.animation = animation
        animationView.contentMode = .scaleAspectFit
        animationView.play()
        animationView.translatesAutoresizingMaskIntoConstraints = false
        animationView.loopMode = loopMode
        view.addSubview(animationView)
        NSLayoutConstraint.activate([
            animationView.heightAnchor.constraint(equalTo: view.heightAnchor),
            animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
        ])
        return view
    }
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
    }
}

Check these before submitting:

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

This issue is a:

  • [v] 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
  • [v] iOS

What Language are you in?

  • [v] Swift
  • [] Objective-C

Code Example

Animation JSON

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (3 by maintainers)

Most upvoted comments

animationView.backgroundBehavior = .pauseAndRestore

animationView.backgroundBehavior = .pauseAndRestore

This solved the problem. I think this issued can be marked as invalid.

pauseAndRestore is the default behavior starting in Lottie 4.0 and later