2012-03-31 91 views
0

我有這個代碼繪製一個窗口和NSView,並設置了跟蹤鼠標進入和退出它增加/減少我的窗口的大小。問題是當鼠標事件被調用並且寬度和高度的int值增加時,窗口會自動重新繪製到新高度,並將舊高度保留在那裏,我如何移除舊的並且只繪製新的?謝謝!刪除NSView的多個實例

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

它看起來像你每次鼠標進入或退出的時間重新創建你的幫助窗口,當你想要做的是改變它的框架。爲什麼不使用你創建窗口的代碼一次,在你的mouseDown方法中,只需使用setFrame更改幀:顯示:

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

你能澄清一下你的意思嗎?謝謝! – 2012-03-31 19:33:53

+0

當鼠標進入和退出時,是不是隻是改變窗口的大小?如果是這樣,你只需要得到窗口的框架,並從frame.size.width和frame.size.height – rdelmar 2012-03-31 20:21:55

+0

加或減100是的,但我怎麼能做到這一點,而不使用一個int值的方法,然後只是添加到像我這樣做的價值數字?我明白你在什麼地方重新創建窗口,但我不知道如何增加寬度和高度。 – 2012-03-31 20:30:32