0
我发现mouseDown被调用,但不是mouseMoved。它如何让mouseMoved被调用?这里是我的代码:NSView的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()
}
}
这是什么?我怎么能修改代码
p.s. 这里是驱动程序代码:
@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()
}
不明白。你能说更多吗? – Choasgtfe
MouseMouved事件需要他的委托启用工作。所以你必须把这一行放在视图的awakeFromNib,viewWillAppear或其他任何地方。 – StrawHara
在最新的swift中:https://developer.apple.com/reference/appkit/nsview,它被折旧 – Choasgtfe