feat: 调整动画大小

This commit is contained in:
Junhui Chen 2025-09-11 22:06:08 +08:00
parent 098d5b7e6a
commit ccdc46236d
2 changed files with 41 additions and 12 deletions

View File

@ -15,6 +15,7 @@ struct BlindBoxAnimationView: View {
case .ready:
ZStack {
LottieView(name: "ready", isPlaying: true)
Color.clear
.contentShape(Rectangle())
.onTapGesture {
@ -29,8 +30,10 @@ struct BlindBoxAnimationView: View {
Image("Empty")
.resizable()
.scaledToFit()
.padding(40)
}
}
.frame(width: 300, height: 300)
.animation(.easeInOut(duration: 0.5), value: phase)
}
}

View File

@ -14,8 +14,13 @@ struct LottieView: UIViewRepresentable {
self.isPlaying = isPlaying
}
func makeUIView(context: Context) -> LottieAnimationView {
func makeUIView(context: Context) -> UIView {
// 使 LottieAnimationView SwiftUI frame
let container = UIView()
container.clipsToBounds = true
let animationView = LottieAnimationView()
animationView.translatesAutoresizingMaskIntoConstraints = false
// 1: 使
if let animation = LottieAnimation.named(name) {
@ -33,6 +38,17 @@ struct LottieView: UIViewRepresentable {
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)
])
// Coordinator 便 updateUIView
context.coordinator.animationView = animationView
// /
if isPlaying {
animationView.play()
@ -40,19 +56,29 @@ struct LottieView: UIViewRepresentable {
animationView.pause()
}
return animationView
return container
}
func updateUIView(_ uiView: LottieAnimationView, context: Context) {
func updateUIView(_ uiView: UIView, context: Context) {
guard let animationView = context.coordinator.animationView else { return }
// isPlaying /
if isPlaying {
if !uiView.isAnimationPlaying {
uiView.play()
if !animationView.isAnimationPlaying {
animationView.play()
}
} else {
if uiView.isAnimationPlaying {
uiView.pause()
if animationView.isAnimationPlaying {
animationView.pause()
}
}
}
// 使 Coordinator make/update animationView
class Coordinator {
var animationView: LottieAnimationView?
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
}