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