feat: 调整动画大小
This commit is contained in:
parent
098d5b7e6a
commit
ccdc46236d
@ -15,6 +15,7 @@ struct BlindBoxAnimationView: View {
|
|||||||
case .ready:
|
case .ready:
|
||||||
ZStack {
|
ZStack {
|
||||||
LottieView(name: "ready", isPlaying: true)
|
LottieView(name: "ready", isPlaying: true)
|
||||||
|
|
||||||
Color.clear
|
Color.clear
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
@ -29,8 +30,10 @@ struct BlindBoxAnimationView: View {
|
|||||||
Image("Empty")
|
Image("Empty")
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
|
.padding(40)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(width: 300, height: 300)
|
.frame(width: 300, height: 300)
|
||||||
|
.animation(.easeInOut(duration: 0.5), value: phase)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,45 +14,71 @@ struct LottieView: UIViewRepresentable {
|
|||||||
self.isPlaying = isPlaying
|
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()
|
let animationView = LottieAnimationView()
|
||||||
|
animationView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
// 方法1: 直接使用文件名加载
|
// 方法1: 直接使用文件名加载
|
||||||
if let animation = LottieAnimation.named(name) {
|
if let animation = LottieAnimation.named(name) {
|
||||||
animationView.animation = animation
|
animationView.animation = animation
|
||||||
}
|
}
|
||||||
// 方法2: 如果方法1失败,尝试使用文件路径加载
|
// 方法2: 如果方法1失败,尝试使用文件路径加载
|
||||||
else if let path = Bundle.main.path(forResource: name, ofType: "json") {
|
else if let path = Bundle.main.path(forResource: name, ofType: "json") {
|
||||||
let animation = LottieAnimation.filepath(path)
|
let animation = LottieAnimation.filepath(path)
|
||||||
animationView.animation = animation
|
animationView.animation = animation
|
||||||
}
|
}
|
||||||
|
|
||||||
// 配置动画
|
// 配置动画
|
||||||
animationView.loopMode = loopMode
|
animationView.loopMode = loopMode
|
||||||
animationView.animationSpeed = animationSpeed
|
animationView.animationSpeed = animationSpeed
|
||||||
animationView.contentMode = .scaleAspectFit
|
animationView.contentMode = .scaleAspectFit
|
||||||
animationView.backgroundBehavior = .pauseAndRestore
|
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 {
|
if isPlaying {
|
||||||
animationView.play()
|
animationView.play()
|
||||||
} else {
|
} else {
|
||||||
animationView.pause()
|
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 控制播放/暂停
|
// 根据 isPlaying 控制播放/暂停
|
||||||
if isPlaying {
|
if isPlaying {
|
||||||
if !uiView.isAnimationPlaying {
|
if !animationView.isAnimationPlaying {
|
||||||
uiView.play()
|
animationView.play()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if uiView.isAnimationPlaying {
|
if animationView.isAnimationPlaying {
|
||||||
uiView.pause()
|
animationView.pause()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用 Coordinator 在 make/update 周期之间保存 animationView 引用
|
||||||
|
class Coordinator {
|
||||||
|
var animationView: LottieAnimationView?
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeCoordinator() -> Coordinator {
|
||||||
|
Coordinator()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user