How to make a Lottie animation loop
Asked Answered
F

1

13

I have successfully implemented Lottie into my program by giving it its own view. Although once called, the animation only plays once. How would I make it so the animation loops?

LottieView (View with Lottie):

import SwiftUI
import Lottie

struct LottieView: UIViewRepresentable {
    typealias UIViewType = UIView
    var filename: String
    func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
        let view = UIView(frame: .zero)
        
        let animationView = AnimationView()
        let animation = Animation.named(filename)
        animationView.animation = animation
        animationView.contentMode = .scaleAspectFit
        animationView.play()
        
        
        animationView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(animationView)
        
        NSLayoutConstraint.activate([
            animationView.widthAnchor.constraint(equalTo: view.widthAnchor), animationView.heightAnchor.constraint(equalTo: view.heightAnchor)
        ])
        
        
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {
        
    }
    
    
    
    
}

Current Implementation in ContentView:

     HStack {
            
            if isScroll {
                LottieView(filename: "swipeLeft").frame(width: 200, height: 200)
                        .padding(.top, 500)
                        .padding(.leading, 100)
                       
                                 }
            Spacer()
        Button(action: {
           SCLAlertView().showInfo("How to use:", subTitle: "Scroll across the screen to view panels. Then press on a panel to view more information.")
        }) {
            Image(systemName: "info.circle")
            .padding(.trailing, 5)
                .padding(.top, 400)
                       
        }
        
    
        }
Felecia answered 25/6, 2020 at 21:20 Comment(1)
You need a modifier that sets the loopMode property to .loop for your views of type AnimationView.Petroglyph
G
16

You can set loop behavior for play calls. Add this code inside makeUIView(context:)

animationView.loopMode = .loop
Gumm answered 26/6, 2020 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.