2016-09-28 89 views
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() 
} 

回答

0

你必須設置委託:

view.acceptsMouseMovedEvents = YES; 
+0

不明白。你能說更多嗎? – Choasgtfe

+0

MouseMouved事件需要他的委託啓用工作。所以你必須把這一行放在視圖的awakeFromNib,viewWillAppear或其他任何地方。 – StrawHara

+0

在最新的swift中:https://developer.apple.com/reference/appkit/nsview,它被折舊 – Choasgtfe

相關問題