问题描述
我在这里遇到问题.我正在创建这样的NSTrackingArea
:
I've got a problem here. I'm creating a NSTrackingArea
like this:
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self frame] options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:self userInfo:nil];
[self addTrackingArea:area];
[area release];
这很好用.但是,这是一个问题.我将其设置如下:
This works quite fine. However, here's a problem. I have it set up like this:
-(void)mouseEntered:(NSEvent *)event {
[self toggleDetail];
}
-(void)mouseExited:(NSEvent *)event {
[self toggleDetail];
}
toggleDetail
基本上是这样的:
- (void)toggleDetail {
if (!attachedWindow) {
NSPoint buttonPoint = NSMakePoint(NSMidX([conditionImage frame]),
NSMidY([conditionImage frame]));
attachedWindow = [[MAAttachedWindow alloc] initWithView:view
attachedToPoint:buttonPoint
inWindow:[self window]
onSide:12
atDistance:10.0];
//config removed because of irrelevance
[[self window] addChildWindow:attachedWindow ordered:NSWindowAbove];
} else {
[[self window] removeChildWindow:attachedWindow];
[attachedWindow orderOut:self];
[attachedWindow release];
attachedWindow = nil;
}
}
现在这是我的问题.当我关闭MAAttachedWindow
并将鼠标移到窗口上方时,它会打开.花花公子.但是,只有当我将鼠标远离MAAttachedWindow
时,这才是好的.一旦将鼠标移到它上面(仍然在主窗口上),它就会开始猛烈地打开和关闭MAAttachedWindow
.
Now here's my problem. When my MAAttachedWindow
is closed, and I move my mouse over the window, it opens. Dandy. However, it's only good when I keep my mouse away from the MAAttachedWindow
. As soon as I move my mouse over it (while still over the main window) it starts to violently open and close the MAAttachedWindow
.
原因如下:窗口打开后,跟踪区域就会认为我的鼠标不再位于窗口上方,因为弹出窗口位于窗口的前面.但是,一旦它删除了弹出窗口,便认为我的鼠标再次位于其上方,从而再次创建并显示了该弹出窗口.因此,这是一个无休止的循环.
Here's why: As soon as the window opens, the tracking area believes that my mouse isn't over the window anymore because the popup is in front of it. However, as soon as it removes the popup, then it thinks that my mouse is over it again, thus creating and showing the popup once more. Thus it's an endless loop.
我的问题是,是否有办法解决这个问题,除非我的鼠标位于窗口和弹出窗口或类似窗口之外,否则无法关闭窗口.有办法吗?
My question is if there's a way around this, where it can not close the window unless my mouse is outside both the window and the popup or something similar. Is there a way to do this?
推荐答案
请参阅-[NSWindow setIgnoresMouseEvents:].
See -[NSWindow setIgnoresMouseEvents:].
顺便说一句,使用覆盖窗口时要格外小心.它们的困难在于创建它时必须给它绝对坐标,并且存在一个小种族-父窗口可以在获得框架的时间与创建子窗口的时间之间移动.窗口移动是由Window Server完成的,并且可以独立于应用程序完成(这就是为什么当应用程序是沙滩球时可以移动窗口的原因).现在很少会出现这个问题,但是正确地修复它是有可能而且非常困难的.如果您要在调整父窗口的大小时尝试调整其大小或移动其子窗口,那么问题就更大了.
By the way, be very careful with overlay windows. The difficulty with them is that you have to give it absolute coordinates when you create it and there’s a small race—the parent window can be moved between the time that you get its frame, and the time that you create the child window. Window moving is done by the Window Server and can be done independently of the application (that's why you can move a window when the application is beach balling). Now it’s very rare that this would be an issue, but it is possible and quite hard to fix it properly. It’s more of a problem if you’re trying to resize or move a child window when the parent window resizes.
现在,我意识到这可能对您都不适用,但是如果确实如此,您可以考虑使用子窗口的替代方法,我建议您使用它.
Now I realise that none of this might apply to you, but if it does, and you can think of an alternative to using child windows, I would advise you to go with it.
这篇关于有什么办法可以解决这个NSTrackingArea问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!