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 动画稳定与否,取决于资源大小和播放时机的控制。