我想要在Gameview和MenuView之间转换,无论我做什么,我都看不到在模拟器或iPhone中的视图之间的转换.
这就是我在观点之间有条件的地方,也是我提到过渡的地方.
struct gameApp: App {
@ObservedObject var appState = AppState(isGameActive: false)
var body: some Scene {
WindowGroup {
if appState.isGameActive == false {
MenuView()
.environmentObject(appState)
.transition(.move(edge: .trailing))
.animation(.default, value: appState.isGameActive)
} else {
GameView(difficulity: 1)
.transition(.move(edge: .trailing))
.animation(.default, value: appState.isGameActive)
}
}
}
}
这里是切换视图的按钮所在的位置,带有"with Animation"
@EnvironmentObject var appState: AppState
var body: some View {
ZStack {
VStack {
Spacer()
Text("Tic Tac Toe")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.indigo)
Image("MainLogo")
.resizable()
.frame(width: 150, height: 150)
Spacer()
Button("Start") {
withAnimation {
appState.isGameActive.toggle()
}
}
.padding()
.padding(.horizontal, 16.0)
.background(.indigo)
.foregroundColor(Color.white)
.containerShape(RoundedRectangle(cornerRadius: 16))
.font(.headline)
Spacer()
}
}
}
}