cocoa – 在视图外部释放鼠标按钮时,NSView不会收到mouseUp:event

cocoa – 在视图外部释放鼠标按钮时,NSView不会收到mouseUp:event,第1张

概述我有一个自定义NSView子类(例如)以下方法: override func mouseDown(with event: NSEvent) { Swift.print("mouseDown") }override func mouseDragged(with event: NSEvent) { Swift.print("mouseDragged") }override func mouseUp 我有一个自定义NSVIEw子类(例如)以下方法:

overrIDe func mouseDown(with event: NSEvent) { Swift.print("mouseDown") }overrIDe func mouseDragged(with event: NSEvent) { Swift.print("mouseDragged") }overrIDe func mouseUp(with event: NSEvent) { Swift.print("mouseUp") }

只要按下鼠标(按钮),拖动并释放所有视图内部​​,这都可以正常工作.但是,当在视图内按下鼠标时,移动到视图外,然后才释放,我从未收到过mouseUp事件.

P.S.:调用超级实现并没有帮助.

@H_301_18@解决方法 Apple的鼠标事件文档的 Handling Mouse Dragging Operations部分提供了一个解决方案:显然,当使用鼠标跟踪循环跟踪事件时,我们确实收到了mouseUp事件.

以下是文档中示例代码的变体,适用于Swift 3:

overrIDe func mouseDown(with event: NSEvent) {    var keepOn = true    mouseDownImpl(with: event)    // We need to use a mouse-tracking loop as otherwise mouseUp events are not delivered when the mouse button is    // released outsIDe the vIEw.    while true {        guard let nextEvent = self.window?.nextEvent(matching: [.leftMouseUp,.leftMouseDragged]) else { continue }        let mouseLocation = self.convert(nextEvent.locationInWindow,from: nil)        let isInsIDe = self.bounds.contains(mouseLocation)        switch nextEvent.type {        case .leftMouseDragged:            if isInsIDe {                mouseDraggedImpl(with: nextEvent)            }        case .leftMouseUp:            mouseUpImpl(with: nextEvent)            return        default: break        }    }}func mouseDownImpl(with event: NSEvent) { Swift.print("mouseDown") }func mouseDraggedImpl(with event: NSEvent) { Swift.print("mouseDragged") }func mouseUpImpl(with event: NSEvent) { Swift.print("mouseUp") }
总结

以上是内存溢出为你收集整理的cocoa – 在视图外部释放鼠标按钮时,NSView不会收到mouseUp:event全部内容,希望文章能够帮你解决cocoa – 在视图外部释放鼠标按钮时,NSView不会收到mouseUp:event所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1002860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存