2010-07-21 67 views
5

我要复制码头堆栈的背景网格和列表模式。背景是半透明的黑色模糊效果:套用CIFilter背景过滤器时,主窗口是透明

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]]; 
} 

我想不出如何实现这种效果。 (我看了显示服务,看看是否有任何有用的功能,但我看不到任何。)

任何想法?

回答

9

这里有私人API可用。以下是Rob Keniger的示例代码:

在10.5中,您可以使用专用函数'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); 
} 
+0

神奇,非常感谢你! – 2010-07-21 14:07:41

+0

FWIW,这不再显示在优胜美地工作。 :-(CGSNewCIFilterByName()返回kCGErrorNotImplemented。 – 2014-10-25 03:26:20

1

你的代码中有一些错误,这里是刚刚工作的代码:

typedef void * CGSConnection; 
extern OSStatus CGSNewConnection(const void **attributes, CGSConnection * id); 

-(void)enableBlurForWindow:(NSWindow *)window { 

    CGSConnection thisConnection; 
    NSUInteger compositingFilter; 
    NSInteger compositingType = 1 << 0; 

    // Make a new connection to Core Graphics 
    CGSNewConnection(NULL, &thisConnection); 

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

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

然后,使用它:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    [_window setOpaque:NO]; 

    [_window setBackgroundColor: [NSColor colorWithCalibratedHue:0.568 saturation:0.388 brightness:0.941 alpha:0.6]]; 

    [self enableBlurForWindow:_window]; 
} 
+0

尽管我已经添加了CoreGraphics.framework和'#import“CoreGraphics/CoreGraphics.h”,但它不再适用于XCode7.1。代码编译失败,因为它找不到'CGSNewCIFilterByName','CGSSetCIFilterValuesFromDictionary'和'CGSAddWindowFilter'。我也有我的项目设置为10.8 OSX上运行,但随后改为10.10 OSX,看看是否会编译然后,和那也失败了。 – Volomike 2015-12-31 01:40:59

1

过滤器不被使用的根层: 来自文档:

/* An array of filters that are applied to the background of the layer. 
* The root layer ignores this property. Animatable. */ 

@property(copy) NSArray *backgroundFilters;