Objective c 删除NSView的多个实例
我有这段代码,它绘制了一个窗口和NSView,并设置了鼠标进入和退出时的跟踪,以增加/减少窗口的大小。问题是,当调用鼠标事件并且宽度和高度的int值增加时,窗口会将自身重新绘制到新的高度,并将旧窗口保留在那里,如何删除旧窗口并只绘制新窗口?谢谢Objective c 删除NSView的多个实例,objective-c,xcode,cocoa,nsview,Objective C,Xcode,Cocoa,Nsview,我有这段代码,它绘制了一个窗口和NSView,并设置了鼠标进入和退出时的跟踪,以增加/减少窗口的大小。问题是,当调用鼠标事件并且宽度和高度的int值增加时,窗口会将自身重新绘制到新的高度,并将旧窗口保留在那里,如何删除旧窗口并只绘制新窗口?谢谢 - (void)toggleHelpDisplay:(int)value { // Create helpWindow. NSRect mainFrame = [[NSScreen mainScre
- (void)toggleHelpDisplay:(int)value
{
// Create helpWindow.
NSRect mainFrame = [[NSScreen mainScreen] frame];
NSRect helpFrame = NSZeroRect;
float width = 90;
float height = 90;
helpFrame.origin.x = (mainFrame.size.width - width) / 2.0;
helpFrame.origin.y = 200.0;
helpFrame.size.width = width + value;
helpFrame.size.height = height + value;
helpWindow = [[BrightnessView windowWithFrame:helpFrame] retain];
// Configure window.
[helpWindow setReleasedWhenClosed:YES];
[helpWindow setHidesOnDeactivate:NO];
[helpWindow setCanHide:NO];
[helpWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[helpWindow setIgnoresMouseEvents:NO];
// Configure contentView.
NSView *contentView = [helpWindow contentView];
[contentView setWantsLayer:YES];
CATextLayer *layer = [CATextLayer layer];
layer.opacity = 0;
[contentView setLayer:layer];
CGColorRef bgColor = CGColorCreateGenericGray(0.0, 0.6);
layer.backgroundColor = bgColor;
CGColorRelease(bgColor);
layer.string = (shadyEnabled) ? HELP_TEXT : HELP_TEXT_OFF;
layer.contentsRect = CGRectMake(0, 0, 1, 1);
layer.fontSize = 40.0;
layer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
layer.borderColor = CGColorGetConstantColor(kCGColorWhite);
layer.borderWidth = 4.0;
layer.cornerRadius = 4.0;
layer.alignmentMode = kCAAlignmentCenter;
[window addChildWindow:helpWindow ordered:NSWindowAbove];
float helpOpacity = (([NSApp isActive] ? 1 : 0));
[[[helpWindow contentView] layer] setOpacity:helpOpacity];
//track mouse so that once hovered make larger.
self.helpView = contentView;
trackingArea = [[[NSTrackingArea alloc] initWithRect:[self.helpView bounds]
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
owner:self
userInfo:nil] autorelease];
[self.helpView addTrackingArea:trackingArea];
}
- (void)mouseEntered:(NSEvent *)event;
{
NSLog(@"entered");
[self toggleHelpDisplay:+100];
}
- (void)mouseExited:(NSEvent *)event;
{
NSLog(@"exited");
[self toggleHelpDisplay:-100];
}
每次鼠标进入或退出时,您似乎都在重新创建“帮助”窗口,而您所要做的只是更改其框架。为什么不使用创建窗口一次所需的代码,并在mouseDown方法中使用setFrame:display更改框架:
[helpWindow setFrame:NSMakeRect(helpWindow.frame.origin.x,helpWindow.frame.origin.y,helpWindow.size.width +100,helpWindow.size.height +100) display:YES];
你能澄清一下你所说的改变框架是什么意思吗?谢谢当鼠标进出时,你不是在改变窗口的大小吗?如果是这样,您只需要获得窗口的框架,然后从frame.size.width和frame.size.heights中加上或减去100。是的,但是我如何做到这一点,而不使用int-value方法,然后像我那样只添加值number?我明白你的意思,我正在重新创建窗口,但我不知道如何增加宽度和高度。我必须创建一个新的跟踪区域吗?当前跟踪区域仅适用于rect的旧大小,而不是resizedI不这么认为的,因为helpView是窗口的内容视图,我认为它会自动调整大小以填充窗口。