我想以网格和列表模式复制停靠堆栈的背景。背景是半透明的黑色,带有模糊效果:
问题是[CALayer backgroundFilters]仅适用于窗口中的内容,过滤器不适用于其他窗口中的内容。这是我的代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//make window transparent
self.window.backgroundColor = [NSColor clearColor];
[self.window setOpaque:NO];
[self.window setHasShadow:NO];
[self.window setStyleMask:NSBorderlessWindowMask];
//make the content view layer hosting
CALayer *rootLayer = [CALayer layer];
[[self.window contentView] setLayer:rootLayer];
[[self.window contentView] setWantsLayer:YES];
//blur the background contents - NOT WORKING!
[rootLayer setBackgroundColor:CGColorCreateGenericGray(0.0, .716)];
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
[rootLayer setBackgroundFilters:[NSArray arrayWithObject: blurFilter]];
}
我想不出还有什么办法可以达到这种效果。 (我查看了显示服务,看看是否有任何有用的功能,但我看不到任何功能。)
有什么想法吗?
最佳答案
有可用的私有(private) API。以下是 Rob Keniger 的示例代码:
在 10.5 中,您可以使用以下命令将任何核心图像过滤器添加到窗口中 私有(private)函数“CGSAddWindowFilter”。
typedef void * CGSConnectionID;
extern OSStatus CGSNewConnection(const void **attr, CGSConnectionID *id);
- (void)enableBlurForWindow:(NSWindow *)window
{
CGSConnectionID _myConnection;
uint32_t __compositingFilter;
int __compositingType = 1; // Apply filter to contents underneath the window, then draw window normally on top
/* Make a new connection to CoreGraphics, alternatively you could use the main connection*/
CGSNewConnection(NULL , &_myConnection);
/* The following creates a new CoreImage filter, then sets its options with a dictionary of values*/
CGSNewCIFilterByName (_myConnection, (CFStringRef)@"CIGaussianBlur", &__compositingFilter);
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"];
CGSSetCIFilterValuesFromDictionary(_myConnection, __compositingFilter, (CFDictionaryRef)optionsDict);
/* Now just switch on the filter for the window */
CGSAddWindowFilter(_myConnection, [window windowNumber], __compositingFilter, __compositingType );
}
关于cocoa - 当主机窗口透明时应用 CIFilter 背景过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3298490/