xcode mouseMoved 未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7543684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mouseMoved not called
提问by Dominik Seibold
I have a subclassed NSView
which is part of a .xib-file of a subclassed NSDocument
, which gets alive by the default behaviour of NSDocumentController
's openDocument:
method. In this subclassed NSView
I have implemented the methods awakeFromNib
, in which the view's NSWindow
setAcceptsMouseMovedEvents:YES
method is called, and acceptsFirstMouse:
, which returns YES
. But my mouseMoved:
method implementation of my subclassed NSView
doesn't get called when I move the mouse over it. What might be the problem?
我有一个子类NSView
,它是子类的 .xib 文件的一部分NSDocument
,它通过NSDocumentController
的openDocument:
方法的默认行为激活。在这个子类中,NSView
我实现了方法awakeFromNib
,其中NSWindow
setAcceptsMouseMovedEvents:YES
调用了视图的方法,并且acceptsFirstMouse:
返回了YES
。但是当我将鼠标移到它上面时mouseMoved:
,我的子类的方法实现NSView
不会被调用。可能是什么问题?
采纳答案by Michael
I haven't used mouseMoved:
in a real project (I've just played around with it a little). As far as I can tell, mouseMoved:
is only called when your view is the first responder and then not only while the mouse is over your view, but always when the mouse moves. You might be better off using an NSTrackingArea. Check the Cocoa Event Handling Guidefor more information.
我没有mouseMoved:
在真正的项目中使用过(我只是玩过一点)。据我所知,mouseMoved:
仅在您的视图是第一响应者时调用,然后不仅在鼠标在您的视图上时调用,而且始终在鼠标移动时调用。使用 NSTrackingArea 可能会更好。查看Cocoa 事件处理指南了解更多信息。
回答by zeeple
Be sure to request the mouseMoved event is sent:
一定要请求发送 mouseMoved 事件:
NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect |
NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:options
owner:self
userInfo:nil];
回答by jbouwman
As noted by others, an NSTrackingArea
is a good solution, and an appropriate place to install the tracking area is NSView.updateTrackingAreas()
. It isn't necessary to set the containing NSWindow's setAcceptsMouseMovedEvents
property.
正如其他人所指出的, anNSTrackingArea
是一个很好的解决方案,安装跟踪区域的合适位置是NSView.updateTrackingAreas()
。没有必要设置包含 NSWindow 的setAcceptsMouseMovedEvents
属性。
In Swift 3:
在 Swift 3 中:
class CustomView : NSView {
var trackingArea : NSTrackingArea?
override func updateTrackingAreas() {
if trackingArea != nil {
self.removeTrackingArea(trackingArea!)
}
let options : NSTrackingAreaOptions =
[.mouseEnteredAndExited, .mouseMoved, .activeInKeyWindow]
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)")
}
}
回答by Kyle
Just incase anyone else runs into this. I ran into an issue where I was subclassing a subclass and was trying to add a tracking area to both classes (for two different reasons).
以防万一其他人遇到这个。我遇到了一个问题,我正在对一个子类进行子类化,并试图向两个类添加一个跟踪区域(出于两个不同的原因)。
If you are doing something like this, you will need to make sure that your mouseMoved:
, etc call into the super, or only one of your subclasses will receive the message.
如果你正在做这样的事情,你需要确保你的mouseMoved:
等调用进入超级,或者只有你的一个子类会收到消息。
- (void) mouseMoved: (NSEvent*) theEvent
{
// Call the super event
[super mouseMoved: theEvent];
}
回答by Kyle
Deshitified version of @jbouwman s answer:
@jbouwman 的回答的 Deshitified 版本:
override func updateTrackingAreas() {
self.addTrackingArea(NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeInKeyWindow], owner: self, userInfo: nil))
}