SwiftUI:如何制作Lottie动画循环?

时间:2020-06-25 21:20:09

标签: swift swiftui lottie lottie-ios

我已经通过给出自己的观点成功地将Lottie实施到了我的程序中。虽然曾经被调用,但是动画只播放一次。我将如何制作动画循环?谢谢。

LottieView(带有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>) {
        
    }
    
    
    
    
}

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

1 个答案:

答案 0 :(得分:4)

您可以设置播放呼叫的循环行为。将此代码添加到makeUIView(context:)

animationView.loopMode = .loop