'RepeatForever animation cannot update itself in SwiftUI
I have a view which use repeatForever function of Animation. I have 2 issue with it: If I run the code on iOS device as soon as rotate my device the animation would be killed, and if I run my code in macOS, the animation does not update itself with changes of the Window.
My Goal is be able have nice and smooth animation when device change position or Window change size in macOS, not necessarily killing running animation and starting brand new, because the goal is having running animation in both changes.
here my code and gif:
struct ContentView: View {
var body: some View {
RaceCarView()
}
}
struct RaceCarView: View {
@State private var start: Bool = Bool()
var body: some View {
VStack(spacing: .zero) {
ZStack(alignment: start ? .leading : .trailing) {
Color.blue.opacity(0.5)
Text("🏎")
.font(Font.system(size: 100))
.frame(width: 100, height: 100)
.offset(x: start ? -100 : 100, y: 2.0)
.onAppear(perform: { start.toggle() })
}
.frame(height: 100)
.clipped()
Color.black.opacity(0.5)
.frame(height: 10)
}
.animation(Animation.linear(duration: 3.0).repeatForever(autoreverses: false), value: start)
}
}
iOS:
macOS:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


