gpt4 book ai didi

swift - NSView 的 mouseMoved 永远不会被调用

转载 作者:行者123 更新时间:2023-11-30 13:02:44 24 4
gpt4 key购买 nike

我发现 mouseDown 被调用,但没有 mouseMoved。怎么才能调用mouseMoved呢?这是我的代码:

@available(OSX 10.10, *)
class MainView: NSView{
override var acceptsFirstResponder: Bool { return true }

override func draw(_ dirtyRect: NSRect){
titledString.draw(at:NSPoint(x:20,y:40))
}
override func mouseDown(with event: NSEvent){
//print("Mouse down")
printMouseDown()
}
override func mouseMoved(with event: NSEvent){
//print("Mouse down")
super.mouseMoved(with:event)
printMouseMove()
}
}

这是什么?我怎样才能修改代码

附: 这是驱动程序代码:

@available(OSX 10.10, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
func applicationDidFinishLaunching(_ notification: Notification){
window.setContentSize(NSSize(width:200, height:160))
window.styleMask = [.titled, .closable, .miniaturizable]
window.level = 1
window.title = "Test"
window.acceptsMouseMovedEvents = true
window.contentView = MainView()
window.center()
window.makeKeyAndOrderFront(window)
}
}

if #available(OSX 10.10, *){
let app = NSApplication.shared()
let appDelegate = AppDelegate()
app.delegate = appDelegate
app.run()
}

最佳答案

您必须设置委托(delegate):

view.acceptsMouseMovedEvents = YES;

关于swift - NSView 的 mouseMoved 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39744116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com