cocoa - 当主机窗口透明时应用 CIFilter 背景过滤器

标签 cocoa macos core-animation

我想以网格和列表模式复制停靠堆栈的背景。背景是半透明的黑色,带有模糊效果:

Example of dock stack in grid mode http://www.thecustommac.com/wp-content/uploads/2009/09/stack-highlight.jpg

问题是[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/

相关文章:

cocoa - NS通知中心: Why bother calling removeObserver?

objective-c - XCode/Cocoa Mac 更改 View

macos - 我可以使用虚拟机代替物理机进行MacOS设备驱动程序开发吗?

objective-c - Core Animation : set anchorPoint on 10. 8 围绕其中心旋转图层

ios - 使用阴影平滑旋转和更改 UIView 的大小

macos - 您如何制作仅在有限时间内有效的 cocoa 演示应用程序?

php - OSX Mavericks - 使用子域设置 Apache

objective-c - 在 Cocoa/Objective-C 中将错误打印到文件

cocoa - 图层托管的 NSView 是否允许有 subview ?

cocoa - 在 Cocoa/C++ 中使用相对路径加载资源