如何获得半透明模糊背景的窗口

时间:2012-06-24 04:18:48

标签: macos cocoa calayer nswindow

我希望得到一个半透明模糊背景的窗口,就像终端可以做的那样。观看此视频,大约30秒,看看我的意思:http://www.youtube.com/watch?v=zo8KPRY6-Mk

在此处查看图片:http://osxdaily.com/wp-content/uploads/2011/04/mac-os-x-lion-terminal.jpg

我一直在谷歌搜索一个小时,但无法得到任何工作。我相信我需要以某种方式创建一个核心动画层并添加一个背景过滤器,但到目前为止我还没有成功......我只看到了窗口的灰色背景。这是我到目前为止的代码:

代码:

//  Get the content view -- everything but the titlebar.
NSView *theView = [[self window] contentView];
[theView setAlphaValue:0.5];

// Create core animation layer, with filter
CALayer *backgroundLayer = [CALayer layer];
[theView setWantsLayer:YES];
[theView setLayer:backgroundLayer]; 
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
[theView layer].backgroundFilters = [NSArray arrayWithObject:blurFilter];  
[[theView layer] setBackgroundFilters:[NSArray arrayWithObject:blurFilter]];

要做我想做的事情的任何提示或示例? 谢谢!

3 个答案:

答案 0 :(得分:3)

不需要图层和过滤器,NSWindow可以自己完成

[mywindow setOpaque:NO];
[mywindow setBackgroundColor: [NSColor colorWithCalibratedHue:0.0 saturation:0.0 brightness:0.2 alpha:0.5]];

请不要使用此功能,因为它也会将您的标题栏设为alpha(在此处发布以防其他人需要)

[mywindow setOpaque:NO];
[mywindow setBackgroundColor: [NSColor blackColor]];
[mywindow setAlphaValue:0.5];

enter image description here

答案 1 :(得分:3)

透明度使用赵九龙的建议。

对于模糊背景,请使用此

对NSWindow的电话:

[self enableBlurForWindow:self];

功能:

-(void)enableBlurForWindow:(NSWindow *)window
{
    //!!!! Uses private API - copied from http://blog.steventroughtonsmith.com/2008/03/using-core-image-filters-onunder.html

    CGSConnection thisConnection;
    uint32_t compositingFilter;
    int compositingType = 1; // Under the window

    /* Make a new connection to CoreGraphics */
    CGSNewConnection(NULL, &thisConnection);

    /* Create a CoreImage filter and set it up */
    CGSNewCIFilterByName(thisConnection, (CFStringRef)@"CIGaussianBlur", &compositingFilter);
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:2.0] forKey:@"inputRadius"];
    CGSSetCIFilterValuesFromDictionary(thisConnection, compositingFilter, (__bridge CFDictionaryRef)options);

    /* Now apply the filter to the window */
    CGSAddWindowFilter(thisConnection, [window windowNumber], compositingFilter, compositingType);
}

注意:它使用私有API

答案 2 :(得分:0)

对于那些在2017年阅读并使用 Swift 4 并希望更改BG Alpha 的人,您可以将以下内容添加到您的自定义NSWindow课程中:

self.backgroundColor = NSColor.black
self.backgroundColor = NSColor.init(calibratedHue: 0, saturation: 0, brightness: 0, alpha: 0.2)

P.S。我还不需要模糊效果,当我这样做时,我会更新答案