【发布时间】:2021-02-10 10:38:37
【问题描述】:
当我在 NavigationView 中放置显式动画时,作为不良副作用,它会为 NavigationView 内容的初始布局设置动画。无限动画尤其糟糕。有没有办法消除这种副作用?
示例:下面的图像应该是全屏蓝色背景上的动画红色加载器。相反,我得到了一个缩放蓝色背景的无限循环:
import SwiftUI
struct EscapingAnimationTest: View {
var body: some View {
NavigationView {
VStack {
Spacer()
EscapingAnimationTest_Inner()
Spacer()
}
.backgroundFill(Color.blue)
}
}
}
struct EscapingAnimationTest_Inner: View {
@State var degrees: CGFloat = 0
var body: some View {
Circle()
.trim(from: 0.0, to: 0.3)
.stroke(Color.red, lineWidth: 5)
.rotationEffect(Angle(degrees: degrees))
.onAppear() {
withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) {
degrees = 360
}
}
}
}
struct EscapingAnimationTest_Previews: PreviewProvider {
static var previews: some View {
EscapingAnimationTest()
}
}
【问题讨论】: