删除多个NSView实例

时间:2012-03-31 01:30:06

标签: objective-c xcode cocoa nsview

我有这个代码,它绘制一个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];
    }

1 个答案:

答案 0 :(得分:1)

看起来每次鼠标进入或退出时都会重新创建帮助窗口,而您只想更改其框架。为什么不使用你拥有的代码创建一次窗口,而在mouseDown方法中只需用setFrame更改框架:display:

[helpWindow setFrame:NSMakeRect(helpWindow.frame.origin.x,helpWindow.frame.origin.y,helpWindow.size.width +100,helpWindow.size.height +100) display:YES];