ios – LottieAnimation(url: ) not animated first time in swift


I need to current Lottie animation view in swiftUI. However sadly, it would not animate the view when it seems on the view first time. If it disappears, then seems once more, then the animation displayed. I’m on the lookout for the answer of how successfully I can current Lottie animation from URL within the view. The code I’ve written is the next:

struct LottieView : UIViewRepresentable {
    let url: URL
    let animationView: LottieAnimationView
    
    init(url: URL) {
        self.url = url // URL(string: "https://assets5.lottiefiles.com/packages/lf20_GoeyCV7pi2.json")!
        self.animationView = LottieAnimationView(url: url) { _ in
            
        }
    }

    func makeUIView(context: Context) -> some UIView {
        let view = UIView()
        animationView.play()
        animationView.loopMode = .loop
        animationView.contentMode = .scaleAspectFit
        view.addSubview(animationView)
        return view
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {
        uiView.removeAllSubviews()
        uiView.addSubview(animationView)
        animationView.contentMode = .scaleAspectFit
        animationView.translatesAutoresizingMaskIntoConstraints = false
        animationView.heightAnchor.constraint(equalTo: uiView.heightAnchor).isActive = true
        animationView.widthAnchor.constraint(equalTo: uiView.widthAnchor).isActive = true
        animationView.loopMode = .loop
        animationView.animationSpeed = 1.0
        animationView.play()
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles