我现在有点难过,因为我一生都无法弄清楚为什么我的视图没有被动画化进出视图。
我目前正在使用 Xcode 13.1 和 SwiftUI 3.0。我目前正在尝试为从底部向上滑动的视图设置动画,当我们将 state 属性设置为 true 并且将此属性设置为 false 时,视图将向下滑动并从视图中移除。
没什么疯狂的,对我来说似乎很简单/直截了当,但是由于某种原因,当我更改 state 属性时,我根本没有得到动画,这很奇怪。
我觉得 WindowGroup 可能是问题所在,但我无法解释为什么这会影响动画。
下面是我目前正在使用的代码,它的效果不如显示我当前行为的 gif。
import SwiftUI
@main
struct MainApp: App {
@State private var showCard = false
var body: some Scene {
WindowGroup {
TabView {
Button("Show Card") {
withAnimation(.easeInOut(duration: 1)) {
showCard.toggle()
}
}
.tabItem {
Image.docViewFinder
Text("scan",
tableName: LocalisationTable.misc)
}
Text("Breakdowns")
.tabItem {
Image.stack
Text("breakdowns",
tableName: LocalisationTable.misc)
}
Text("Home")
.tabItem {
Image.people
Text("people",
tableName: LocalisationTable.misc)
}
Text("Home")
.tabItem {
Image.gear
Text("settings",
tableName: LocalisationTable.misc)
}
}
.accentColor(.blue)
.overlay(alignment: .bottom) {
if showCard {
Rectangle()
.edgesIgnoringSafeArea(.all)
.transition(.move(edge: .bottom))
.animation(.linear(duration: 1), value: showCard)
.frame(maxWidth: .infinity,
maxHeight: 250)
}
}
}
}
}