1

我的应用程序中有正在进行的动画,这些动画是使用更新属性触发onAppear和配置的。withAnimation@State

每次视图出现时,动画都会比以前快一点,所以如果视图被显示,然后被模态显示覆盖或隐藏在导航中,然后再次出现,动画开始运行非常非常快——可能是 10 或 20比它应该的要快几倍。

这是代码...

struct HueRotationAnimation: ViewModifier {
    @State var hueRotationValue: Double
    func body(content: Content) -> some View {
        content
            .hueRotation(Angle(degrees: hueRotationValue))
            .onAppear() {
                DispatchQueue.main.async {
                    withAnimation(.linear(duration: 20).repeatForever(autoreverses: false)) {
                        hueRotationValue += 360
                    }
                }
            }
    }
}

struct GradientCircle: View {
    var gradient: Gradient
    @State var hueRotationValue: Double = Double.random(in: 0..<360)
    
    var body: some View {
        GeometryReader { geometry in
            Circle()
                .fill(
                    radialGradient(geometry: geometry, gradient: gradient)
                )
                .modifier(HueRotationAnimation(hueRotationValue: hueRotationValue))
        }
    }
}

func radialGradient(geometry: GeometryProxy, gradient: Gradient) -> RadialGradient {
    RadialGradient(gradient: gradient,
                   center: .init(x: 0.82, y: 0.85),
                   startRadius: 0.0,
                   endRadius: max(geometry.size.width, geometry.size.height) * 0.8)
}

知道每次视图重新出现时导致加速的原因吗?有什么建议可以解决这个问题吗?

(注意:这是运行 Xcode 13.0 beta 4)

4

2 回答 2

2

我认为这与您的 += 360 有关,因为每次它出现时,它需要旋转的度数再增加 360 度。不要在出现时添加 360,而是尝试为动画应该运行的时间设置一个状态布尔值。试试下面的代码,看看它是否适合你。

struct HueRotationAnimation: ViewModifier {
@State var hueRotationValue: Double
@State private var animated = false

func body(content: Content) -> some View {
    content
        .hueRotation(Angle(degrees: animated ? hueRotationValue : hueRotationValue + 360)).animation(.linear(duration: 20).repeatForever(autoreverses: false))
        .onAppear() {
            self.animated.toggle()
        }
}
}

这样 360 度动画应该保持 360 度并且动画的速度不应该改变。

于 2021-08-03T07:14:22.253 回答
1

为了推进,@yawnobleix 的回答,我添加了一个 .onDisappear 切换。当视图出现>消失>出现时,它解决了一些其他错误。

struct HueRotationAnimation: ViewModifier {
    @State var hueRotationValue: Double
    @State private var animated = false
    func body(content: Content) -> some View {
        content
            .hueRotation(Angle(degrees: animated ? hueRotationValue : hueRotationValue + 360)).animation(.linear(duration: 20).repeatForever(autoreverses: false))
            .onAppear {
                animated = true
            }
            .onDisappear {
                animated = false
            }
    }
}
于 2021-08-03T18:30:08.517 回答