我正在制作一個影片,它從螢屏底部顯示先前占據螢屏的視圖部分上方的視圖。我的代碼在技術上可以正常作業,但我擔心影片看起來太斷斷續續。基本上,我認為正在發生的事情是,新的、上升的視圖由其他幾個視圖組成,當我影片它出現時,它也會影片化聚集在一起的子視圖 - 我不喜歡它的外觀。
示例代碼:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
VStack {
Button(action: { withAnimation(.linear(duration: 0.5)) { show = !show }} ) {
Text("Press Me")
}
Rectangle()
.foregroundColor(.gray)
}
}
if show {
VStack {
CollapsibleView()
}
}
}
}
struct CollapsibleView: View {
var body: some View {
ZStack {
VStack {
Text("Text 1")
Text("Text 2")
Text("Text 3")
}
.background(Color.white)
}
}
}
請注意,出于說明目的,持續時間設定得相當長,但即使在較小的持續時間值下,我仍然可以注意到斷斷續續的效果。
我如何避免這種情況?有沒有辦法讓動作影片化?
uj5u.com熱心網友回復:
這是您可能正在尋找的方法:
struct ContentView: View {
@State var show: Bool = Bool()
var body: some View {
VStack {
Button(action: { show.toggle() }, label: { show ? Text("hide") : Text("show") })
.animation(nil)
Color.gray
Group {
if show { CollapsibleView().transition(.asymmetric(insertion: .move(edge: .bottom), removal: .move(edge: .bottom))) }
}
.opacity(show ? 1.0 : 0.0)
}
.animation(Animation.spring(response: 0.4, dampingFraction: 0.4, blendDuration: 1.0), value: show)
}
}
struct CollapsibleView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Text 1")
Text("Text 2")
Text("Text 3")
}
.background(Color.white)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324367.html
