32 lines
1.1 KiB
Swift
32 lines
1.1 KiB
Swift
import SwiftUI
|
||
import Lottie
|
||
|
||
/// 仅播放一次并在完成时回调的 Lottie 视图
|
||
struct BlindBoxLottieOnceView: UIViewRepresentable {
|
||
let name: String
|
||
var animationSpeed: CGFloat = 1.0
|
||
let onCompleted: () -> Void
|
||
|
||
func makeUIView(context: Context) -> LottieAnimationView {
|
||
let animationView = LottieAnimationView()
|
||
if let animation = LottieAnimation.named(name) {
|
||
animationView.animation = animation
|
||
} else if let path = Bundle.main.path(forResource: name, ofType: "json") {
|
||
let animation = LottieAnimation.filepath(path)
|
||
animationView.animation = animation
|
||
}
|
||
animationView.loopMode = .playOnce
|
||
animationView.animationSpeed = animationSpeed
|
||
animationView.contentMode = .scaleAspectFit
|
||
animationView.backgroundBehavior = .pauseAndRestore
|
||
animationView.play { _ in
|
||
onCompleted()
|
||
}
|
||
return animationView
|
||
}
|
||
|
||
func updateUIView(_ uiView: LottieAnimationView, context: Context) {
|
||
// 单次播放,不需要在更新时重复触发
|
||
}
|
||
}
|