精选文章

08. Lottie 动画

2019-06-03 · Lottie

08. Lottie 动画

目标:加载稳定、状态可控、资源可管理。

一、安装(SPM)

https://github.com/airbnb/lottie-ios

二、基础使用

import Lottie

final class SuccessView: UIView {
    private let animationView = LottieAnimationView()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let animation = LottieAnimation.named("success")
        animationView.animation = animation
        animationView.contentMode = .scaleAspectFit
        animationView.loopMode = .playOnce

        addSubview(animationView)
        animationView.frame = bounds
        animationView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        animationView.play()
    }

    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}

三、控制播放

animationView.play(fromProgress: 0, toProgress: 1, loopMode: .playOnce) { finished in
    print("finished: \(finished)")
}

animationView.stop()

四、性能要点

  • 列表中避免同时播放多个动画
  • 资源过大要拆分
  • 不再显示时及时停止

Lottie 动画稳定与否,取决于资源大小和播放时机的控制。

JJ

作者简介

专注于内容创作、产品策略与设计实践。欢迎交流合作。

上一篇 下一篇