From 7adf475b8fa811c3b54d914f1e60c2409d1a3e44 Mon Sep 17 00:00:00 2001 From: Junhui Chen Date: Fri, 12 Sep 2025 14:51:49 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=92=AD=E6=94=BE=E5=8A=A8=E7=94=BB?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/BlindBoxLottieOnceView.swift | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/wake/Features/BlindBox/Components/BlindBoxLottieOnceView.swift b/wake/Features/BlindBox/Components/BlindBoxLottieOnceView.swift index f77c32d..c7d6eaa 100644 --- a/wake/Features/BlindBox/Components/BlindBoxLottieOnceView.swift +++ b/wake/Features/BlindBox/Components/BlindBoxLottieOnceView.swift @@ -7,25 +7,43 @@ struct BlindBoxLottieOnceView: UIViewRepresentable { var animationSpeed: CGFloat = 1.0 let onCompleted: () -> Void - func makeUIView(context: Context) -> LottieAnimationView { + 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 animationView + + return container } - func updateUIView(_ uiView: LottieAnimationView, context: Context) { + func updateUIView(_ uiView: UIView, context: Context) { // 单次播放,不需要在更新时重复触发 } }