wake-ios/wake/Features/BlindBox/Components/BlindBoxLottieOnceView.swift
2025-09-12 15:06:39 +08:00

85 lines
2.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import Lottie
/// Lottie
struct BlindBoxLottieOnceView: UIViewRepresentable {
let name: String
var animationSpeed: CGFloat = 1.0
let onCompleted: () -> Void
func makeUIView(context: Context) -> UIView {
// LottieView
let container = UIView()
container.clipsToBounds = true
let animationView = LottieAnimationView()
animationView.translatesAutoresizingMaskIntoConstraints = false
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
container.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.leadingAnchor.constraint(equalTo: container.leadingAnchor),
animationView.trailingAnchor.constraint(equalTo: container.trailingAnchor),
animationView.topAnchor.constraint(equalTo: container.topAnchor),
animationView.bottomAnchor.constraint(equalTo: container.bottomAnchor)
])
//
animationView.play { _ in
onCompleted()
}
return container
}
func updateUIView(_ uiView: UIView, context: Context) {
//
}
}
#if DEBUG
struct BlindBoxLottieOnceView_Previews: PreviewProvider {
static var previews: some View {
Group {
VStack(spacing: 16) {
Text("Opening • x1.0")
.font(.caption)
.foregroundColor(.gray)
BlindBoxLottieOnceView(name: "opening", animationSpeed: 1.0) {
print("Opening completed")
}
.frame(width: 300, height: 300)
.background(Color.themeTextWhiteSecondary)
}
.previewDisplayName("Opening • 1.0x")
VStack(spacing: 16) {
Text("Opening • x1.5")
.font(.caption)
.foregroundColor(.gray)
BlindBoxLottieOnceView(name: "opening", animationSpeed: 1.5) {
print("Opening completed (1.5x)")
}
.frame(width: 300, height: 300)
.background(Color.themeTextWhiteSecondary)
}
.previewDisplayName("Opening • 1.5x")
}
.padding()
.background(Color.themeTextWhiteSecondary)
}
}
#endif