SwiftUI:如何使视图随着滑动到顶部而消失?

SwiftUI: how to make a view disappear with a slide to the top?

我想通过过渡到顶部使视图消失,但我不知道如何使用 SwiftUI 执行此操作。到目前为止,这是我的代码:

struct MyList: View {
    @State private var loading = true
    @State private var elements: [MyElement] = []

    var body: some View {
        ZStack {
            Color.white
            List(elements) {
                ListRow([=10=])
            }
            if loading {
                LoadingView() // This is the view I want to slide to the top when hiding
                    .ignoresSafeArea()
            }
        }
        .onAppear {
            callAPI() { elements in
                self.elements = elements
                self.loading = false
            }
        }
    }
}

我想隐藏 LoadingView() 与滑动到顶部的过渡,但不知道如何。

感谢您的帮助!

您可以使用 .transition 修饰符。

documentation

LoadingView()
     .transition(.move(edge: .top))

但不要忘记为其设置动画:

.onAppear {
        self.loading = true
        callAPI() { elements in
            self.elements = elements

            DispatchQueue.main.async { // as this is probably from background Thread dispatch it
               self.loading = false
             }
        }
    }.animation(.default, value: loading)