当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Remove Multiple instances of NSView

问题描述:

I have this code which draws a Window and NSView, and have set up tracking to when mouse entered and exits it increases/decreases the size of my window. The problem is when the mouse events are called and the int value of the width and height are increased, the window redraws itself to the new hight and leaves the old one there, how can I remove the old one and just draw the new one? thanks!

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

}

网友答案:

It looks like you're recreating your help window every time your mouse enters or exits, when all you want to do is change its frame. Why not use the code you have to create the window once, and in your mouseDown method just change the frame with setFrame:display:

[helpWindow setFrame:NSMakeRect(helpWindow.frame.origin.x,helpWindow.frame.origin.y,helpWindow.size.width +100,helpWindow.size.height +100) display:YES];
分享给朋友:
您可能感兴趣的文章:
随机阅读: