gpt4 book ai didi

objective-c - 删除 NSView 的多个实例

转载 作者:行者123 更新时间:2023-12-03 17:58:36 29 4
gpt4 key购买 nike

我有这段代码,它绘制一个 Window 和 NSView,并设置了对鼠标进入和退出时的跟踪,它会增加/减小窗口的大小。问题是,当调用鼠标事件并且宽度和高度的int值增加时,窗口将自身重绘到新的高度并将旧的保留在那里,如何删除旧的并只绘制新的?谢谢!

- (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];

关于objective-c - 删除 NSView 的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952233/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com