Here is the minimal example:
struct ContentView: View {
@State var offset: CGFloat = 0
@State var opacity: Double = 1
var body: some View {
Text("Hello, world!")
.opacity(opacity)
.offset(y: offset)
.onAppear {
withAnimation(.easeInOut.repeatForever()) {
offset = 30
}
withAnimation(.easeInOut.repeatForever()) {
opacity = 0
}
}
}
}
Both offset and opacity work fine; however, if I rotate the phone, opacity keeps animating (the words keep appearing and disappearing), but offset does not (the words stop jumping up and down).
Is it a bug? Is there some workaround?
Thanks.
Update: I can get notifications about device rotations using NotificationCenter
and restart animation with an update.toggle()
trick. It seems kinda hacky though. Maybe there is a better way?