我的应用程序中有正在进行的动画,这些动画是使用更新属性触发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)