我弄亂了 Swift UI 的線性影片技術,并注意到與我的預期相反,增加持續時間似乎并沒有使影片發生得更慢。這是故意的嗎?如果是這樣,我該如何制作較慢的影片?
示例代碼:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
withAnimation(.linear(duration: 50)) {
CollapsibleView()
}
}
}
Button(action: { show = !show }) {
Text("Press Me")
}
}
}
struct CollapsibleView: View {
var body: some View {
VStack {
Text("Text 1")
Text("Text 2")
Text("Text 3")
}
}
}
@main
struct app: App {
var body: some Scene {
WindowGroup {
ButtonView()
}
}
}
嘗試更改持續時間引數,看看您是否能注意到較慢的影片。我高達 5000(我假設這是以秒為單位測量的?)并且它仍然以看似相同的速度進行影片處理。
uj5u.com熱心網友回復:
您已將 放置withAnimation在視圖層次結構中。你真正想要的地方是在Button's 行動中:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
CollapsibleView()
}
}
Button(action: {
withAnimation(.linear(duration: 10)) {
show.toggle()
}
}) {
Text("Press Me")
}
}
}
另外,還可以使用.animation在ZStack:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
CollapsibleView()
}
}
.animation(.linear(duration: 10), value: show)
Button(action: {
show.toggle()
}) {
Text("Press Me")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324368.html
