当我按下按钮时,我希望第二个矩形(绿色)在第一个矩形(红色)上上下移动
struct ContentView: View {
@State var visible = false
var body: some View {
return HStack {
Button("button") {
self.visible.toggle()
}
ZStack {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.red)
if visible {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.green)
.transition(.move(edge: .bottom))
.animation(.linear(duration: 2))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当绿色矩形出现时,它出现在红色矩形上方,但当它消失时,移动过渡出现在红色矩形后面。
小智 5
只需添加zIndex
:
struct ContentView: View {
@State var visible = false
var body: some View {
return HStack {
Button("button") {
self.visible.toggle()
}
ZStack {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.red)
if visible {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.green)
.transition(.move(edge: .bottom))
.animation(.linear(duration: 2))
.zIndex(1)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
62 次 |
最近记录: |