简体   繁体   English

将文件拖动到Cocoa的WKWebView上的可拖动视图时,光标没有正确更改

[英]Cursor isn’t changed properly when drag a file to a draggable view on Cocoa's WKWebView

I wrote the following code to put a draggable view on WKWebView. 我编写了以下代码,以将可拖动视图放在WKWebView上。
With this code, I expected a "+" icon will be displayed nearby a cursor when I dragged a file to the view. 使用此代码,当我将文件拖动到视图时,我希望光标附近会显示一个“ +”图标。

class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let rect = NSRect(x: 0, y: 0, width: 200, height: 200)

        let webView = WKWebView(
            frame: rect,
            configuration: WKWebViewConfiguration())
        webView.load(URLRequest(
            url: URL(string: "https://i.imgur.com/D5ru3Q7.jpg")!))

        let draggableView = DraggableView(frame: rect)
        draggableView.registerForDraggedTypes([.fileURL])

        self.view.addSubview(webView)
        self.view.addSubview(draggableView)
    }
}

class DraggableView: NSView {
    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
        return .copy
    }
}

The result is here: 结果在这里:

在此处输入图片说明

  • Sometimes the cursor changes to a magnifying glass (same as a mouse over behavior on WKWebView). 有时光标会变成放大镜(与将鼠标悬停在WKWebView上的行为相同)。
  • And sometimes "+" icon is shown. 有时会显示 “ +”图标。

I think the webView prevents the cursor changing. 我认为webView可以防止更改光标。 So I tried the followings. 因此,我尝试了以下方法。 But I couldn't fix. 但是我无法解决。

  • Overriding hitTest of the webView worked only for non-dragging mouse over actions. 覆盖webView的hitTest仅适用于非拖动鼠标悬停操作。 But not for dragging. 但不是为了拖拖拉拉。
  • webView.unregisterDraggedTypes() didn't work. webView.unregisterDraggedTypes()不起作用。

Is there anyway to fix this? 有没有什么办法解决这一问题?

Found a solution: 找到了解决方案:

for t in webView.trackingAreas {
    webView.removeTrackingArea(t)
}

I think removing tracking areas on a web view is not a good idea. 我认为删除Web视图上的跟踪区域不是一个好主意。 But at least it satisfies my need. 但这至少可以满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cocoa Swift:WKWebView在将其添加为子视图时未显示 - Cocoa Swift: WKWebView is not showing when is add it as subview 将UIView插入视图控制器视图的层次结构时,它不位于集合视图单元格的顶部 - A UIView isn't placed right on top of a collection view cell when inserted to the view controller view's hierarchy 当SwiftUI View设置为MapAnnotation的内容时,View改变时frame没有正确更新 - When SwiftUI View is set to the content of MapAnnotation, the frame does not update properly when View is changed 在 SwiftUI 视图内创建拖动手柄(用于可拖动窗口等) - Creating a drag handle inside of a SwiftUI view (for draggable windows and such) 为什么未列出UITableViewCell的分隔符视图? - Why isn't UITableViewCell's Separator View listed? WKWebView 没有完成加载,当 didFinishNavigation 被调用时 - WKWebView 中的错误? - WKWebView didn't finish loading, when didFinishNavigation is called - Bug in WKWebView? 用户的纬度和经度未传递到新的视图控制器 - User's latitude and longitude isn't passed to new view controller 实际显示视图时的WKWebView通知 - WKWebView notification when view is actually shown layoutSubviews vs frame didSet用于检测视图的框架何时更改 - layoutSubviews vs frame didSet to detect when the view's frame changed 如何检测 UIPageViewController 的视图何时更改 - How to detect when a UIPageViewController's view has changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM