根據檔案,它應該非常簡單。例如List:https : //developer.apple.com/documentation/swiftui/list/ondrop(of : istargeted : perform :) -75hvy#
對UTType應限制什么SwiftUI物件可以接收引數。就我而言,我只想接受Apps。的UTType是.applicationBundle:https://developer.apple.com/documentation/uniformtypeidentifiers/uttype/3551459-applicationbundle
但它不起作用。SwiftUI 物件永遠不會改變狀態,也永遠不會接受放置。關閉永遠不會運行。無論是Lists, H/VStacks, Buttons, 還是什么。該pdf型別似乎也不起作用,以及許多其他型別。我可以使用的唯一型別 if fileURL,主要是沒有限制。
我不確定我是否做錯了什么,或者 SwiftUI 是否在 mac 上作業了一半。
這是代碼:
List(appsToIgnore, id: \.self, selection: $selection) {
Text($0)
}
.onDrop(of: [.applicationBundle, .application], isTargeted: isTargeted) { providers in
print("hehe")
return true
}
更換或只是增加.fileURL了在UTType陣列使得下降的作業,但沒有任何型別的限制。
我也試著使用.onInsert上ForEach,而不是(https://developer.apple.com/documentation/swiftui/foreach/oninsert(of:perform:)-2whxl#),并且要經過適當的DropDelegate(HTTPS:/ /developer.apple.com/documentation/swiftui/dropdelegate#)但一直得到相同的結果。macOS 的 SwiftUI drop 似乎還沒有作業,但我找不到任何關于此的官方資訊。在檔案中它是macOS 11.0 這樣寫的,所以我希望它可以作業?
任何資訊表示贊賞!謝謝。
uj5u.com熱心網友回復:
您需要手動驗證,使用DropDelegate拖過的檔案型別。
這是可能的方法的簡化演示。使用 Xcode 13 / macOS 11.6 測驗
let delegate = MyDelegate()
...
List(appsToIgnore, id: \.self, selection: $selection) {
Text($0)
}
.onDrop(of: [.fileURL], delegate: delegate) // << accept file URLs
和驗證部分像
class MyDelegate: DropDelegate {
func validateDrop(info: DropInfo) -> Bool {
// find provider with file URL
guard info.hasItemsConforming(to: [.fileURL]) else { return false }
guard let provider = info.itemProviders(for: [.fileURL]).first else { return false }
var result = false
if provider.canLoadObject(ofClass: String.self) {
let group = DispatchGroup()
group.enter() // << make decoding sync
// decode URL from item provider
_ = provider.loadObject(ofClass: String.self) { value, _ in
defer { group.leave() }
guard let fileURL = value, let url = URL(string: fileURL) else { return }
// verify type of content by URL
let flag = try? url.resourceValues(forKeys: [.contentTypeKey]).contentType == .applicationBundle
result = flag ?? false
}
// wait a bit for verification result
_ = group.wait(timeout: .now() 0.5)
}
return result
}
func performDrop(info: DropInfo) -> Bool {
// handling code is here
return true
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342721.html
上一篇:SwiftUI-在macOS版本的情況下應用修飾符是一些特定的
下一篇:模擬全域鍵盤按下
