swiftui - SwiftUI onDrop 没有被调用?
问题描述
SwiftUI 的 drop API 只在特定条件下触发?
尝试将“Drag Me”块拖到其他块上。
出于某种原因,aTextEditor
需要满足 2 个条件,然后它的“superview”才能检测到下降?
- 有一个
background
- 禁用
struct ContentView: View {
@StateObject fileprivate var viewModel = ViewModel()
@State private var s1 = "Nope"
@State private var s2 = "Nope"
@State private var s3 = "Nope"
@State private var s4 = "I can detect"
@State private var s5 = "I can detect"
@State private var s6 = "Drag me"
var body: some View {
VStack(spacing: 10) {
TextEditor (text: $s1) // drop not detected
.disabled(true)
TextEditor (text: $s2) // drop not detected
.opacity(0.5)
.disabled(true)
TextEditor (text: $s3) // drop not detected
.opacity(0.5).background(Color.pink.opacity(0.5))
TextEditor (text: $s4) // drop detected
.background(Color.pink.opacity(0.5))
.disabled(true)
TextEditor (text: $s5) // drop detected
.opacity(0.5).background(Color.pink.opacity(0.5))
.disabled(true)
Text("Yes") // drop detected
.background(Color.pink.opacity(0.5))
TextEditor (text: $s6)
.onDrag { () -> NSItemProvider in
return NSItemProvider(object: "Hello" as NSString)
}
}.onDrop(of: [.plainText], delegate: viewModel)
}
}
fileprivate class ViewModel: NSObject, ObservableObject, DropDelegate {
func dropEntered(info: DropInfo) {
print("Drop entered: \(info)")
}
func dropUpdated(info: DropInfo) -> DropProposal? {
print("Drop updated: \(info)")
return DropProposal(operation: .move)
}
func dropExited(info: DropInfo) {
print("Drop exited: \(info)")
}
func performDrop(info: DropInfo) -> Bool {
print("Perform drop: \(info)")
return true
}
}
在 Xcode 12.3、iOS 14.3 上测试
我错过了什么还是这是一个错误?
解决方案
推荐阅读
- c++ - 您可以在类和构造函数中进行初始化吗?对吗?
- python - Python Pandas:将值应用于 groupby 结果
- android - 如何使用 ProGuard 去除 Timber.tag.(...).d(...) 格式的日志?
- javascript - 单击添加到购物车以触发弹出窗口
- c - 如何使输入不超过指定长度?
- sql - 插入到仅包含标识列的表中
- python - 在 python 中为 Panda 数据框的每一行创建一个雷达图
- php - 为什么 error.com 将我重定向到我的 wamp 服务器 localhost?
- python - 如何在 iOS 上长按?
- javascript - OnClick Javascript 函数在 React 中不起作用