我有一個子類NSView
它是一個子類NSDocument
的.xib文件的一部分,它通過NSDocumentController
的openDocument:
方法的默認行爲得到活動。在這個小類NSView
我已經實現了方法awakeFromNib
,其中視圖的NSWindow
setAcceptsMouseMovedEvents:YES
方法被調用,和acceptsFirstMouse:
,它返回YES
。但我的mouseMoved:
方法實現我的子類NSView
不會被調用,當我將鼠標移到它上面。可能是什麼問題?mouseMoved not called
回答
我還沒有在一個真正的項目中使用mouseMoved:
(我剛剛玩了一下)。據我所知,mouseMoved:
只在您的視圖是第一響應者時才被調用,然後不僅當鼠標位於視圖上時,而且總是在鼠標移動時。使用NSTrackingArea可能會更好。檢查Cocoa Event Handling Guide獲取更多信息。
一定要要求的mouseMoved事件發送:
NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect |
NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:options
owner:self
userInfo:nil];
一個音符。我不確定它應該是一個int,而是一個NSInteger類型或typedef,最好是NSTrackingAreaOptions,它的可讀性更高,保證是正確的類型。 – uchuugaka
這實際上是正確的答案 –
只是櫃面任何人運行到這一點。我遇到了一個問題,我繼承了一個子類,並試圖向兩個類添加跟蹤區域(出於兩個不同的原因)。
如果你正在做這樣的事情,你需要確保你的mouseMoved:
等調用超級,或者只有一個你的子類會收到消息。
- (void) mouseMoved: (NSEvent*) theEvent
{
// Call the super event
[super mouseMoved: theEvent];
}
正如其他人所指出的,NSTrackingArea
是一個很好的解決方案,並安裝跟蹤區域是NSView.updateTrackingAreas()
一個合適的位置。沒有必要設置包含NSWindow的setAcceptsMouseMovedEvents
屬性。
在斯威夫特3:
class CustomView : NSView {
var trackingArea : NSTrackingArea?
override func updateTrackingAreas() {
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
let options : NSTrackingAreaOptions =
[.activeWhenFirstResponder, .mouseMoved ]
trackingArea = NSTrackingArea(rect: self.bounds, options: options,
owner: self, userInfo: nil)
self.addTrackingArea(trackingArea!)
}
override func mouseMoved(with event: NSEvent) {
Swift.print("Mouse moved: \(event)")
}
}
- 1. cellForRowAtIndexPath not called但numberOfRowsInSection called
- 2. ActionMode OnCreateActionMode not called
- 3. draggingEntered not called
- 4. viewDidAppear not called
- 5. UIButton Action Not Called
- 6. jainsip processResponse not called
- 7. MFMailComposeViewController not called
- 8. QGraphicsItem paint not called
- 9. java keylistener not called
- 10. trigger.io --forge.geolocation.getCurrentPosition()not called
- 11. Java PaintComponent not Called
- 12. QApplication :: notify not called
- 13. didFailToReceiveAdWithError not called
- 14. Youtube Iframe:onYouTubePlayerAPIReady()not called
- 15. 「mapView:didUpdateUserLocation:」not called
- 16. onAudioFocusChange not called
- 17. cellForRowAtIndexPath not called(again)
- 18. SessionListener sessionDestroyed not called
- 19. Template.destroyed()not called
- 20. UICollectionView cellForItem not called
- 21. ccTouchesBegin not called
- 22. paintComponent not called
- 23. Swift viewWillTransition not called
- 24. onActivityResult not beeing called
- 25. AjaxButton onSubmit not called
- 26. TYPO3 updateAction not called
- 27. Angular:ErrorInterceptor not beeing called
- 28. AsyncTask:onCancelled(Result)not called
- 29. uploadify onError not called
- 30. RowStyleSelector Not Called
根據你的使用情況,您可以使用的mouseDragged。沒有NSTrackingArea的情況下,它只能在鼠標關閉時觸發。 –