我有一個NSView的子類,它包含以下NSTrackingArea代碼。但由於某些原因,鼠標事件不會在Playground中觸發。遊戲場中的鼠標事件(NSTrackingArea)不起作用
viewWillMoveToWindow
正在被調用,但沒有其他東西似乎火。有沒有人知道缺失的線索?
class MyView: NSView {
private var trackingArea: NSTrackingArea = NSTrackingArea()
// Other stuff omitted here...
// ...
override func viewWillMoveToWindow(newWindow: NSWindow?) {
// Setup a new tracking area when the view is added to the window.
trackingArea = NSTrackingArea(rect: self.bounds, options: [.MouseEnteredAndExited, .ActiveAlways], owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
}
override func updateTrackingAreas() {
self.removeTrackingArea(trackingArea)
trackingArea = NSTrackingArea(rect: self.bounds, options: [.MouseEnteredAndExited, .ActiveAlways], owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
}
// Mouse events
override func mouseEntered(theEvent: NSEvent) {
NSLog("MouseEntered")
}
override func mouseExited(theEvent: NSEvent) {
NSLog("MouseExited")
}
override func mouseDown(theEvent: NSEvent) {
NSLog("MouseDown")
}
}
爲什麼你不和一個真實的項目測試嗎?或者嘗試導入XCPlayground XCPlaygroundPage.currentPage.needsIndefiniteExecution = true –
我知道它在一個真實的項目中起作用,但想要使用Playground來測試一些繪圖工作。 'XCPlaygroundPage.currentPage.needsIndefiniteExecution = true'沒有成功。 XCPlayground已經被導入。 – d00dle