[英]SwiftUI transitions between views, no transition
I want to have a transition between a gameView and a menuView, and no matter what I do I do not see a transition between the views either in a simulator or with my iPhone.我想在 gameView 和 menuView 之间进行转换,无论我做什么,我都看不到模拟器或 iPhone 中的视图之间的转换。
That's where I have conditions between views, and where I mention the transition.这就是我在视图之间有条件的地方,也是我提到过渡的地方。
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)
}
}
}
}
Here is where the button that switch the view is, with a "withAnimation"这是切换视图的按钮所在的位置,带有“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()
}
}
}
}
Put it condition into animatable container, like将其条件放入可动画容器中,例如
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 !!
}
*however I would recommend to separate everything related to views into root view so scene would look like *但是我建议将与视图相关的所有内容分离到根视图中,以便场景看起来像
WindowGroup {
ContentView() // << this !!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.