I want the second rectangle (green) come up and down over the first rectangle (red) when I press the button
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))
}
}
}
}
}
When the green rectangle appears it comes up over the red rectangle, but when it disappears the the moving transition appears behind the red rectangle.
Just add the 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)
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.