SwiftUI: Broken explicit animations in NavigationView?

后端 未结 1 1471
情深已故
情深已故 2021-01-19 17:14

When I put an explicit animation inside a NavigationView, as an undesirable side effect, it animates the initial layout of the NavigationView content. It gets especially bad

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 17:26

    Here is fixed part. Tested with Xcode 12 / iOS 14.

    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: Double(degrees)))
                .animation(Animation.linear(duration: 1).repeatForever(autoreverses: false), value: degrees)
                .onAppear() {
                        DispatchQueue.main.async {
                        degrees = 360
                        }
                }
        }
    }
    

    Update: the same will be using withAnimation

    .onAppear() {
        DispatchQueue.main.async {
            withAnimation(Animation.linear(duration: 1).repeatForever(autoreverses: false)) {
               degrees = 360
            }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题