我一直在 SwiftUI 1.0 中开发一个项目,因为它需要支持 iOS 13.0 设备(所以不可fullScreenCover
用)。我要求我的应用程序在我的视图之间有干净的过渡动画,所以我在GeometryReader
整个应用程序中广泛使用,如下所示:
现在我已经完成了 80% 的 UI,我正在尝试集成数据获取部分。所以,我所做的是onAppear
在我的视图上调用我的方法中的数据获取方法。这是我的代码:
struct ContentView: View {
@State private var isUserLoggedIn = false
var body: some View {
GeometryReader { geometry in
HomeView()
LoginView(isUserLoggedIn: $isUserLoggedIn)
.offset(y: isUserLoggedIn ? geometry.size.height + geometry.safeAreaInsets.bottom : 0)
}
}
}
struct LoginView: View {
@Binding var isUserLoggedIn: Bool
var body: some View {
Button("Login") {
withAnimation {
isUserLoggedIn.toggle()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)
.edgesIgnoringSafeArea(.all)
.onAppear {
print("Login appeared!")
}
}
}
struct HomeView: View {
@State private var isTwoSelected = false
var body: some View {
GeometryReader { geometry in
OneView(isTwoSelected: $isTwoSelected)
.offset(x: isTwoSelected ? -geometry.size.width : 0)
TwoView(isTwoSelected: $isTwoSelected)
.offset(x: isTwoSelected ? 0 : geometry.size.width)
}
}
}
struct OneView: View {
@Binding var isTwoSelected: Bool
var body: some View {
NavigationView {
Button("Goto Two") {
withAnimation {
isTwoSelected.toggle()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.edgesIgnoringSafeArea(.all)
.navigationBarTitle(Text("One"))
.onAppear {
print("One appeared! Fetch data...")
}
}
}
}
struct TwoView: View {
@Binding var isTwoSelected: Bool
var body: some View {
NavigationView {
Button("Goto One") {
withAnimation {
isTwoSelected.toggle()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.yellow)
.edgesIgnoringSafeArea(.all)
.navigationBarTitle(Text("Two"))
.onAppear {
print("Two appeared! Fetch data...")
}
}
}
}
但我现在面临的问题是它们是同时被触发的。可以看到控制台打印如下:
Login appeared!
Two appeared! Fetch data...
One appeared! Fetch data...
仅当它们出现在设备的视图框架中时,我将如何将数据提取到相应的视图?