0

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?

MigMit
  • 1,698
  • 12
  • 14
  • This is expected with SwiftUI it doesn’t work well with animations that cross values (offset needs position and those values are changing with rotation). Maybe a bug too or a limitation. – lorem ipsum Feb 17 '23 at 19:34

0 Answers0