我有一個黃色的容器,里面有綠色的景色。我想移動容器,同時隱藏/顯示內部綠色視圖,并帶有影片。目前,我正在使用

這是我的代碼:
struct ContentView: View {
@State var showingSubview = false
var body: some View {
VStack {
Button("Show Subview") {
withAnimation(.easeInOut(duration: 2)) {
showingSubview.toggle()
}
}
if showingSubview {
Text("Subview")
.padding()
.background(Color.green)
}
}
.padding()
.background(Color.yellow)
.offset(x: showingSubview ? 150 : 0, y: 0)
}
}
當綠色視圖淡入淡出時,如何使綠色視圖與黃色容器一起移動?最好,我想繼續使用if或switch宣告插入/洗掉。
uj5u.com熱心網友回復:
您可以在影片播放時更改高度。
代碼版本 #1
這不會褪色并出現在黃色矩形內。
代碼:
struct ContentView: View {
@State var showingSubview = false
var body: some View {
VStack(spacing: 0) {
Button("Show Subview") {
withAnimation(.easeInOut(duration: 2)) {
showingSubview.toggle()
}
}
Text("Subview")
.padding()
.background(Color.green)
.padding(.top)
.frame(height: showingSubview ? nil : 0, alignment: .top)
.clipped()
}
.padding()
.background(Color.yellow)
.offset(x: showingSubview ? 150 : 0, y: 0)
}
}
結果#1

代碼版本 #2
此版本將淡出并出現在底部邊緣,如您的 GIF 所示。
代碼:
struct ContentView: View {
@State var showingSubview = false
var body: some View {
VStack(spacing: 0) {
Button("Show Subview") {
withAnimation(.easeInOut(duration: 2)) {
showingSubview.toggle()
}
}
Text("Subview")
.padding()
.background(Color.green)
.padding(.top)
.frame(height: showingSubview ? nil : 0, alignment: .top)
.padding(.bottom)
.background(Color.yellow)
.clipped()
.opacity(showingSubview ? 1 : 0)
}
.padding([.horizontal, .top])
.background(Color.yellow)
.padding(.bottom)
.offset(x: showingSubview ? 150 : 0, y: 0)
}
}
結果#2

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/375491.html
標籤:ios 迅速 迅捷 swiftui-动画
