在 iOS 中添加 Lottie 动画

Adding Lottie Animation in iOS

我是 Lottie 库的新手,正在尝试集成一个简单的动画。我的动画 json 位于文件夹 Resources->Json->LottieAnimations->checkMark.json

我已经通过 pod install 安装了 Lottie,它的当前版本是 3.1.0。 在我的情节提要中,我添加了一个视图并将 class 名称命名为 AnimationView,在界面构建器中将动画名称命名为“checkMark”。

这是出口

@IBOutlet weak var checkMarkAnimationView: AnimationView!

在 viewDidLoad 上,我只是调用

checkMarkAnimationView.play()

但是这里什么也没有发生。我错过了什么吗?

你需要做这样的事情(如果在代码中):

 let view = AnimationView()
    let path = Bundle.main.path(forResource: "checkMark",
                                ofType: "json") ?? ""
    view.animation = Animation.filepath(path)
    self.view.addSubview(view)
    view.play()

如果不起作用,请检查动画名称或JSON文件

已更新

还要检查 JSON 文件添加到 Bundle 资源中(构建阶段 -> 复制 Bundle 资源)

  let animationView = AnimationView()
 let path = Bundle.main.path(forResource: "loader",
                                     ofType: "json") ?? ""
  animationView.animation = Animation.filepath(path)
        animationView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        animationView.center = self.view.center
        animationView.loopMode = .loop
        self.view.addSubview(animationView)
        animationView.play()
    let animationView = AnimationView()
    let animation = Animation.named("<place-your-json-filename>", animationCache: LRUAnimationCache.sharedCache)
    self.view.addSubview(animationView)
    animationView.frame = self.view.bounds
    animationView.animation = animation
    animationView.loopMode = .loop
    animationView.play()
let animationSubView = AnimationView(name: "jsonAnimationFileName")
animationSubView.frame = self.checkMarkAnimationView.bounds
animationSubView.contentMode = .scaleAspectFit

self.checkMarkAnimationView.addSubview(animationSubView)
animationSubView.play()

将此添加到 viewDidLoad()