我想讓一個視圖消失并過渡到頂部,但我不知道如何使用 SwiftUI 做到這一點。到目前為止,這是我的代碼:
struct MyList: View {
@State private var loading = true
@State private var elements: [MyElement] = []
var body: some View {
ZStack {
Color.white
List(elements) {
ListRow($0)
}
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(),但不知道如何。
感謝您的幫助!
uj5u.com熱心網友回復:
你可以使用.transition修飾符。
檔案
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476578.html
