在埃爾卡皮坦在的Xcode 7測試使用夫特2.0,我一個子類如NSView
NSCollectionView
的項目視圖的原型圖的使用,並重寫updateTrackingAreas:
方法做鼠標跟蹤。 NSCollectionView
在NSPopover
內。updateTrackingAreas:覆蓋僅適用於前2次?
調試日誌顯示,似乎只會調用updateTrackingAreas:
的前2次。該代碼是這樣的:
override func updateTrackingAreas() {
Swift.print("updateTrackingAreas:")
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
trackingArea = NSTrackingArea(
rect: self.bounds,
options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways],
owner: self,
userInfo: nil
)
if trackingArea != nil {
self.addTrackingArea(trackingArea!)
}
var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
super.updateTrackingAreas()
}
第2次酥料餅被打開時,我可以看到控制檯日誌顯示updateTrackingAreas:
。然後跟蹤失敗,也沒有日誌。
編輯:
當我註釋掉這一部分,如:
//var mouseLocation = self.window?.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation!, fromView: nil)
if CGRectContainsPoint(self.bounds, mouseLocation!) {
mouseEntered(NSEvent())
} else {
mouseExited(NSEvent())
}
將不再存在的問題。
而下面的代碼是沒有區別:
if let window = self.window {
var mouseLocation = window.mouseLocationOutsideOfEventStream
mouseLocation = self.convertPoint(mouseLocation, fromView: nil)
if let event = NSApplication.sharedApplication().currentEvent {
if NSPointInRect(mouseLocation, self.bounds) {
mouseEntered(event)
} else {
mouseExited(event)
}
}
}
編輯2:
好吧,我只是重新安裝OS X Yosemite和在Xcode 7測試版1,上述問題不再重新編譯它存在。可能只是El Capitan的一個bug。我會把它報告給蘋果公司。謝謝你們。
爲什麼你需要在這個方法中進行命中測試?爲什麼不能以通常的方式處理mouseEntered和mouseExited事件? –
因爲當你在視圖上滾動時,'mouseEntered:'和'mouseExited:'將被忽略。問題不是關於我爲什麼這麼做的原因,而是爲什麼它沒有在第三次工作。 –
你做這件事的原因很重要,因爲你的代碼沒有按你期望的方式工作。您的問題的答案可能是您的問題無論如何都是錯誤的思路(即您的方法和/或假設可能有缺陷)。不要對自己想要的答案進行防禦 - 或者更糟糕 - 固執己見 - 用我們對自己期望的相互尊重來對待我們,並回答我們自己的澄清要求。 –