ios - SwiftUI中的.transition修饰符

标签 ios swift swiftui

当我按下按钮时,我希望第二个矩形(绿色)在第一个矩形(红色)上方上下移动

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))
                }

            }
        }
    }
}

当绿色矩形出现时,它会出现在红色矩形上方,但是当它消失时,移动的过渡会出现在红色矩形后面。

enter image description here

最佳答案

只需添加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)
                }

            }
        }
    }
}

关于ios - SwiftUI中的.transition修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60223939/

相关文章:

swift - 如何在 SwiftUI 中禁用“显示标签栏”菜单选项

ios - SwiftUI:如何更改 TabbedView 中所选项目的图像

ios - 带有常规文本的 SwiftUI 低不透明度按钮

ios - Monotouch 中的 DequeueReusableCell 导致单元格在滚动时重新排列

ios - iOS6 中的 ASIHTTPRequest

ios - 当上下文为 nil 时,Swift Temp CoreData 对象无法设置 ivars

swift - 在 alamofire 4.0 和 iOS 10 Swift 中使用 Google Map Directions API

ios - 信号量在执行其后的代码块之前不会等待

ios - 未上传开发 APNs 证书

iOS 10 服务扩展不一致