RectangleView with TextView slide into screen in 3 seconds, but state of TextView changed after a second while sliding. Now you can see TextView stops sliding, jumps to the destination at once.
I Just want the RectangleView with the TextView as a whole when sliding, and do not change TextView's position whatever it rotates or keeps still when state changed.
GIF dmeo:
https://stackoverflow.com/questions/65795434/swiftui-view-with-animation-jumps-to-destination-when-state-changed
I Just want the RectangleView with the TextView as a whole when sliding, and do not change TextView's position whatever it rotates or keeps still when state changed.
GIF dmeo:
https://stackoverflow.com/questions/65795434/swiftui-view-with-animation-jumps-to-destination-when-state-changed
Code Block import SwiftUI struct RotationEnvironmentKey: EnvironmentKey { static let defaultValue: Bool = false } extension EnvironmentValues { var rotation: Bool { get { return self[RotationEnvironmentKey] } set { self[RotationEnvironmentKey] = newValue } } } struct AnimationTestView: View { @State private var go = false @State private var rotation = false var body: some View { VStack { Button("Go!") { go.toggle() DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { rotation.toggle() print("set rotation = \(rotation)") } } Group { if go { RectangleView() .transition(.slide) .environment(\.rotation, rotation) } }.animation(.easeInOut(duration: 3.0), value: go) }.navigationTitle("Animation Test") } } struct RectangleView: View { var body: some View { Rectangle() .frame(width: 200, height: 100) .foregroundColor(.pink) .overlay(TextView()) } } struct TextView: View { @Environment(\.rotation) var rotation @State private var animationRotating: Bool = false let animation = Animation.linear(duration: 3.0).repeatForever(autoreverses: false) var body: some View { print("refresh, rotation = \(rotation)"); return HStack { Spacer() if rotation { Text("R") .foregroundColor(.blue) .rotationEffect(.degrees(animationRotating ? 360 : 0)) .animation(animation, value: animationRotating) .onAppear { animationRotating = true } .onDisappear { animationRotating = false } } else { Text("S") } } } }