[英]SwiftUI transitions between views, no transition
我想在 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)
}
}
}
}
這是切換視圖的按鈕所在的位置,帶有“withAnimation”
@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()
}
}
}
}
將其條件放入可動畫容器中,例如
WindowGroup {
ZStack { // << here
if appState.isGameActive == false {
MenuView()
.environmentObject(appState)
.transition(.move(edge: .trailing))
} else {
GameView(difficulity: 1)
.transition(.move(edge: .trailing))
}
}
.animation(.default, value: appState.isGameActive) // << here !!
}
*但是我建議將與視圖相關的所有內容分離到根視圖中,以便場景看起來像
WindowGroup {
ContentView() // << this !!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.