类似Mac应用商店的工具栏有噪音



我发现了NSWindow的这个很棒的子类,但它不会给工具栏的渐变添加任何噪音。如果你仔细观察应用商店、Reeder或推特,它们都有渐变噪音。

如何将噪波添加到渐变中?

我找到了这个线程,但我不明白如何将其放入代码中。

首先,nessseary代码已经添加到INAppStoreWindow中,所以我不再有这个用例了。然而,对于那些想知道如何做到这一点的人来说,以下是INAppStoreWindow是如何做到的。

首先,创建一个用于制作带有噪声的图像的函数。

static CGImageRef createNoiseImageRef(NSUInteger width, NSUInteger height, CGFloat factor)
{
    NSUInteger size = width*height;
    char *rgba = (char *)malloc(size); srand(124);
    for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256*factor;}
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapContext = 
    CGBitmapContextCreate(rgba, width, height, 8, width, colorSpace, kCGImageAlphaNone);
    CFRelease(colorSpace);
    free(rgba);
    CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
    CFRelease(bitmapContext);
    return image;
}

然后使用图像将噪声覆盖在当前图形上

    static CGImageRef noisePattern = nil;
    if (noisePattern == nil) noisePattern = createNoiseImageRef(128, 128, 0.015);
    [NSGraphicsContext saveGraphicsState];
    [[NSGraphicsContext currentContext] setCompositingOperation:NSCompositePlusLighter];
    CGRect noisePatternRect = CGRectZero;
    noisePatternRect.size = CGSizeMake(CGImageGetWidth(noisePattern), CGImageGetHeight(noisePattern));        
    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextDrawTiledImage(context, noisePatternRect, noisePattern);
    [NSGraphicsContext restoreGraphicsState];

最新更新