类似iTunes的状态栏

时间:2014-10-06 12:18:28

标签: macos cocoa graphics

EDITED: 我试图展示像iTunes一样的信息栏。前面详细介绍了这个主题,例如在iTunes or Xcode style information box at top of window 我只是稍微修改了上面引用的链接中的代码,所以让它在最近的XCode下编译。 我的代码如下:

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
    static NSShadow *kDropShadow = nil;
    static NSShadow *kInnerShadow = nil;
    static NSGradient *kBackgroundGradient = nil;
    static NSColor *kBorderColor = nil;

    if (kDropShadow == nil) {
        kDropShadow = [[NSShadow alloc] init];
        [kDropShadow setShadowColor:[NSColor colorWithCalibratedWhite:.863 alpha:.75]];
        [kDropShadow setShadowOffset:NSMakeSize(0.0, -1.0)];
        [kDropShadow setShadowBlurRadius:1.0];
        kInnerShadow = [[NSShadow alloc] init];
        [kInnerShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.52]];
        [kInnerShadow setShadowOffset:NSMakeSize(0.0, -1.0)];
        [kInnerShadow setShadowBlurRadius:4.0];
        kBorderColor = [[NSColor colorWithCalibratedWhite:0.569 alpha:1.0] retain];
        // iTunes style

        // kBackgroundGradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor colorWithCalibratedRed:0.929 green:0.945 blue:0.882 alpha:1.0],0.0,[NSColor colorWithCalibratedRed:0.902 green:0.922 blue:0.835 alpha:1.0],0.5,[NSColor colorWithCalibratedRed:0.871 green:0.894 blue:0.78 alpha:1.0],0.5,[NSColor colorWithCalibratedRed:0.949 green:0.961 blue:0.878 alpha:1.0],1.0, nil];

        // Xcode style
        kBackgroundGradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor colorWithCalibratedRed:0.957 green:0.976 blue:1.0 alpha:1.0],0.0,[NSColor colorWithCalibratedRed:0.871 green:0.894 blue:0.918 alpha:1.0],0.5,[NSColor colorWithCalibratedRed:0.831 green:0.851 blue:0.867 alpha:1.0],0.5,[NSColor colorWithCalibratedRed:0.82 green:0.847 blue:0.89 alpha:1.0],1.0, nil];
    }

    NSRect bounds = [self bounds];

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:bounds xRadius:3.5 yRadius:3.5];

    [NSGraphicsContext saveGraphicsState];
    [kDropShadow set];
    [path fill];
    [NSGraphicsContext restoreGraphicsState];

    [kBackgroundGradient drawInBezierPath:path angle:-90.0];

    [kBorderColor setStroke];
    [path stroke];
}
但是,它不起作用。我不认为drawRect()方法会被调用。我错过了什么?请指教。

谢谢

1 个答案:

答案 0 :(得分:0)

我最后有一条额外的线:     [路径填充]; 删除它可以解决问题。