lvo*_*mer 3 macos swift swiftui
我们将OnDrop()SwiftUI 中的函数用于 MacOS 应用程序。它真的很棒。但是,我只想onDrop在特殊条件下允许该功能。我尝试仅在该条件为真时才在 onDrag 之后执行代码,这工作正常。但是,仍然存在不应该可见的拖动动画/鼠标拖动效果。
那是我们正在使用的代码:
.onDrop(of: [“public.file-url”], isTargeted: $userData.shopPopOver) { providers -> Bool in
for provider in providers
{
Run Code Online (Sandbox Code Playgroud)
我只能.onDrop在条件下添加。只是为了展示一个例子,这肯定是行不通的:
if (condition)
{
.onDrop(of: [“public.file-url”], isTargeted: $userData.shopPopOver) { providers -> Bool in
Run Code Online (Sandbox Code Playgroud)
提前致谢!
这是可用于描述的用例的自定义修饰符
struct Droppable: ViewModifier {
let condition: Bool
let types: [String]
let tracking: Binding<Bool>?
let action: ([NSItemProvider]) -> Bool
@ViewBuilder
func body(content: Content) -> some View {
if condition {
content.onDrop(of: types, isTargeted: tracking, perform: action)
} else {
content
}
}
}
extension View {
public func acceptDrop(if condition: Bool, of supportedTypes: [String], isTargeted: Binding<Bool>?, perform action: @escaping ([NSItemProvider]) -> Bool) -> some View {
self.modifier(Droppable(condition: condition, types: supportedTypes, tracking: isTargeted, action: action))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |