Удалить несколько экземпляров NSView - PullRequest
0 голосов
/ 31 марта 2012

У меня есть этот код, который рисует окно и 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 Ответ

1 голос
/ 31 марта 2012

Похоже, что вы воссоздаете свое окно справки каждый раз, когда мышь входит или выходит, когда все, что вы хотите сделать, это изменить его фрейм.Почему бы не использовать код, который вы должны создать окно один раз, а в вашем методе mouseDown просто изменить кадр с помощью setFrame: display:

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