我試圖在嘗試匯出檔案時顯示 ProgressView。該檔案是一個大檔案,我正在使用fileExporter修飾符。在 ZStack 中有一個 ProgressView 并將 zIndex 設定為 1 不起作用。我還嘗試將長操作(獲取檔案)放入 DispatchQueue 但挑戰在于它不是視圖并且無法編譯。下面是我目前擁有的代碼。
這是我所期望的:在我按下“匯出檔案”按鈕后,我應該在創建檔案時看到一個旋轉的圓圈(在這種情況下是模擬長時間操作的 2 秒睡眠命令)。之后應該顯示fileExporter的“移動”表
很感謝任何形式的幫助。
這是一個示例應用程式:
struct ContentView: View {
@State private var isExporting: Bool = false
var body: some View {
VStack {
Button(action: {
isExporting = true
}, label: {
Text("Export File")
})
.padding()
if isExporting {
ProgressView() {
ZStack{}
.fileExporter(isPresented: $isExporting, document: FileExport(contents: "This is a simple Text"), contentType: .plainText) { result in
if case .success = result {
print(try! result.get())
print("File Saved")
} else {
print("File Not Saved")
}
}
}
}
}
}
}
struct FileExport: FileDocument {
static var readableContentTypes: [UTType] {[.plainText]}
var contents: String
init(contents: String) {
self.contents = contents
sleep(2) // simulate a long operation
}
init(configuration: ReadConfiguration) throws {
contents = ""
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
return FileWrapper(regularFileWithContents: contents.data(using: .utf8)!)
}
}
uj5u.com熱心網友回復:
您的代碼中存在一些問題。首先,您ProgressView()實際上并不包含在ZStack. ZStack您顯示的唯一內容不包含任何內容,因為內容必須在花括號內。在這種情況下,您通常可以將整個視圖包裝在ZStack.
接下來,您自己的視圖通常不會采用這樣的尾隨閉包。您沒有包含您的ProgressView代碼,但是您展示的代碼作為尾隨閉包是荒謬的,所以我認為這不是您的意圖。因此,我洗掉了開式花括號和適當的閉式花括號。
當所有這些都說完并完成后,視圖將顯示,但它仍將位于檔案匯出視窗下方。我從未找到任何方法將視圖放置在這樣的作業系統表上方。如果它是您的作業表,我已經看到了一些“解決方案”,但并不真正推薦它們。
另外,我不確定您如何獲取資料以顯示進度視圖,而不僅僅是作為連續微調器。我在檔案中找不到 Apple 允許您訪問該資料的任何地方。
最后,因為您將進度視圖鍵入到isPresentedvarisExporting意味著您ProgressView()將在作業FileExport表執行時關閉。
此外,雖然它似乎與.fileExporter條件if塊內部一起作業,但我感覺不對,我會將它移到視圖的另一部分。這似乎是一種不好的做法。
FWIW,這是“作業”代碼,但我不知道您如何實際實作目標:
struct FileExporter: View {
@State private var isExporting: Bool = false
var body: some View {
ZStack {
VStack {
Button(action: {
isExporting = true
}, label: {
Text("Export File")
})
.padding()
if isExporting {
ProgressView() // { No brace would go here
.fileExporter(isPresented: $isExporting, document: FileExport(contents: "This is a simple Text"), contentType: .plainText) { result in
if case .success = result {
print(try! result.get())
print("File Saved")
} else {
print("File Not Saved")
}
}
}
}
}
}
}
如果您想實作一個顯示進度視圖(條形圖或微調器)的解決方案,我認為您必須自己動手。
uj5u.com熱心網友回復:
這是一個可能的解決方案。可能有更好的方法來做到這一點!我正在使用 aZStack{}附加fileExporter. 有沒有不同的方法或其他解決方案?
struct ContentView: View {
@State private var isExporting: Bool = false
@State private var isLoading: Bool = false
@State private var document: FileExport? = nil
var body: some View {
VStack {
Button(action: {
isExporting = true
isLoading = true
DispatchQueue.global(qos: .background).async {
document = FileExport(contents: "This is a simple text")
DispatchQueue.main.async {
isLoading = false
}
}
}, label: {
Text("Export File")
})
.padding()
if isLoading {
ProgressView()
.zIndex(1.0)
}
if isExporting && !isLoading {
ZStack {}
.fileExporter(isPresented: $isExporting, document: document, contentType: .plainText) { result in
if case .success = result {
print(try! result.get())
print("File Saved")
} else {
print("File Not Saved")
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313644.html
