添加 Lottie 全局加载层并修复加载状态卡住问题
This commit is contained in:
51
suixinkan/UI/Loading/LottieLoadingView.swift
Normal file
51
suixinkan/UI/Loading/LottieLoadingView.swift
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// LottieLoadingView.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import Lottie
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// Lottie 循环 Loading 动画视图,使用 `Resources/Lottie/loading.json`。
|
||||
final class LottieLoadingView: UIView {
|
||||
|
||||
private let animationView: LottieAnimationView
|
||||
|
||||
override init(frame: CGRect) {
|
||||
animationView = LottieLoadingView.makeAnimationView()
|
||||
super.init(frame: frame)
|
||||
addSubview(animationView)
|
||||
animationView.snp.makeConstraints { make in
|
||||
make.edges.equalToSuperview()
|
||||
}
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
/// 开始循环播放。
|
||||
func startAnimating() {
|
||||
animationView.loopMode = .loop
|
||||
animationView.play()
|
||||
}
|
||||
|
||||
/// 停止播放。
|
||||
func stopAnimating() {
|
||||
animationView.stop()
|
||||
}
|
||||
|
||||
private static func makeAnimationView() -> LottieAnimationView {
|
||||
if let url = loadingAnimationURL() {
|
||||
return LottieAnimationView(filePath: url.path)
|
||||
}
|
||||
return LottieAnimationView()
|
||||
}
|
||||
|
||||
private static func loadingAnimationURL() -> URL? {
|
||||
Bundle.main.url(forResource: "loading", withExtension: "json", subdirectory: "Resources/Lottie")
|
||||
?? Bundle.main.url(forResource: "loading", withExtension: "json")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user