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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)