如何获得窗口与半透明模糊的背景 [英] how to get window with semi-transparent blurred background
问题描述
我想得到一个有半透明模糊背景的窗口,就像终端可以做的一样。观看此视频约30秒钟,看看我的意思: http://www.youtube.com/watch ?v = zo8KPRY6-Mk
I'd like to get a window that has a semi-transparent blurred background, just like what the Terminal can do. See this video, about 30 sec in, to see what I mean: http://www.youtube.com/watch?v=zo8KPRY6-Mk
在这里查看图片:http://osxdaily.com/wp-content/uploads/2011/04/mac-os-x-lion-terminal.jpg
我已经搜索了一个小时,不能工作。我相信我需要以某种方式创建一个核心动画层和添加一个背景过滤器,但我到目前为止没有成功...我只是看到我的窗口的灰色背景。下面是我到目前为止的代码:
I've been googling for an hour, and can't get anything to work. I believe I need to somehow create a core animation layer and add a background filter, but I've been unsuccessful so far... I just see the gray background of my window. Here's the code I've got so far:
代码:
// 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]];
任何提示或示例来做我想做的事情?
谢谢!
Any tips or examples to do what I'm trying to do? Thanks!
推荐答案
透明度使用Jiulong Zhao的建议。
For the transparency use Jiulong Zhao's suggestion.
对于模糊的背景,请使用
For a blurred background use this
在NSWindow上调用:
The call on a 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);
}
NB:它使用私人API
NB: It uses a private API
这篇关于如何获得窗口与半透明模糊的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!