我在這里創建了一個文本影片。目前這個影片是由按鈕觸發的。如何重新設計代碼,使其自動啟動而不是由按鈕執行。該按鈕應在最后完全洗掉。
struct ContentView: View {
@State var shortString = true
var body: some View {
VStack {
Text(shortString ? "This is short." : "This is considerably longer.").font(.title)
.animation(.easeInOut(duration:1.0))
Button(action: {self.shortString.toggle()}) {
Text("Toggle").padding()
}
}
}
}
uj5u.com熱心網友回復:
您可以使用onAppear在視圖出現時立即觸發影片:
struct ContentView: View {
@State var shortString = true
var body: some View {
VStack {
Text(shortString ? "This is short." : "This is considerably longer.").font(.title)
.animation(.easeInOut(duration:1.0), value: shortString)
}.onAppear {
shortString.toggle()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/363660.html
