Hello fellow SwiftUI'ers! 🤓
In my project I have the root
view which conditionally displays either full-screen logo image (loading mode), or main view of the app, based on whether required data was loaded.
When root view appears, it initiates data loading via onReceive
and updates local @State
property with received data. That triggers root view update and the main view displayed instead.
I can't make main view to appear with slide
transition (or any other transition, that is) - no matter how I do it. I have added .transition(.slide)
to both of my views and enclosed @State
property update in withAnimation
, but still the main view always appears immediately. 😕
I have tried moving .transition
modifier around, using DispatchQueue.main.async
to update @State
property, enclosing my views in different type of container - absolutely nothing does any difference. I bet I'm missing something fundamental here and would be very grateful of any kind of help! 🙏
Thanks a lot!
Here is greatly simplified version of my root view.
struct RootView: View {
@State private var data: [String]
var body: some View {
return Group {
Group {
if !data.isEmpty {
NavigationView {
MainView(with: data)
}.transition(.slide)
} else {
ZStack {
Color.clear
Image(uiImage: Images.oxygenLogo.image)
}.transition(.slide)
}
}
}.onReceive(service.loadData().receive(on: DispatchQueue.main)) { data in
withAnimation {
self.data = data
}
}
}
}