CALayer过滤器涵盖其他子层

时间:2011-01-17 04:19:34

标签: cocoa core-animation calayer core-image

我正在处理自助服务终端类型的应用程序。我想在中间有一个标志(有效)。现在我正在尝试在徽标周围添加一些浮动图层。我可以绘制图层(在drawDots中),但是当我添加一个过滤器时 - 它会覆盖我绘制的徽标图层。我在这里复制了所有重要的代码。同样,如果我在dotLayer上注释掉设置过滤器数组的线,那么点图层的紫色方块和徽标是可见的,但是当我应用过滤器时 - 徽标不可见。我真的不明白,因为我正在设置这些应该是适当的帧。我还尝试用dotLayer上的子图层做一些事情,看看是否有帮助。

- (void)awakeFromNib
{
 // Initialization code here.
 // The cursor isn't used for selection, so we hide it
 [NSCursor hide];

 // go full screen, as a kiosk application 
 [self enterFullScreenMode:[self.window screen] withOptions:NULL];

 [self setupLayers];

 [self drawDots];

 // Make the window the first responder to get keystrokes
 [self.window makeFirstResponder:self];

 // bring the window to the front
 [self.window makeKeyAndOrderFront:self];
}

- (void)drawDots
{
 CGColorRef purpleColor = CGColorCreateGenericRGB(0.604, 0.247, 0.463, 1.0);
 CGColorRef transparentColor = CGColorCreateGenericRGB(0, 0, 0, 1.0);

// CALayer *something = [CALayer layer];
// something.frame = NSMakeRect(15, 15, 60, 60); 
 CALayer *dotLayer = [CALayer layer];
 dotLayer.frame = NSMakeRect(15, 15, 30, 30);
 dotLayer.backgroundColor = purpleColor;

 CIFilter *gradientFilter = [CIFilter filterWithName:@"CIGaussianGradient"];
 [gradientFilter setDefaults];
 [gradientFilter setValue:(id)[CIColor colorWithCGColor:purpleColor] forKey:@"inputColor0"];
 [gradientFilter setValue:(id)[CIColor colorWithCGColor:transparentColor] forKey:@"inputColor1"];
 [gradientFilter setValue:(id)[NSNumber numberWithFloat:20.0] forKey:@"inputRadius"];

// dotLayer.compositingFilter = gradientFilter;

 dotLayer.filters = [NSArray arrayWithObject:(id)gradientFilter];

 [rootLayer addSublayer:dotLayer];
// [dotLayer addSublayer:something];
// 
// [rootLayer addSublayer:dotLayer];
}

- (void)setupLayers
{
 // Setup vars
 CGFloat mirroredOffset = 10.0f;

 CGColorRef blackColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
 CGColorRef transparentColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 0.0);

 // Get the image
 logo = [NSImage imageNamed:@"logo_300.png"]; 

 // Get the positioning of the logo - middle of the screen
 CGFloat logoWidth = [logo size].width;
 CGFloat logoHeight = [logo size].height;

 CGFloat imageX = [self frame].size.width / 2 - logoWidth / 2;
 CGFloat imageY = [self frame].size.height / 2 - logoHeight / 2;

 // Create the layer for the all content to be drawn on
 rootLayer = [CALayer layer];
 rootLayer.geometryFlipped = YES;
 rootLayer.opaque = YES;
 rootLayer.frame = [self frame];
 rootLayer.backgroundColor = blackColor;
 [self setLayer:rootLayer];

 NSRect imageRect = NSMakeRect(imageX, imageY, logoWidth, logoHeight * 2 + mirroredOffset);

 // Create the layer that holds the logo
 CALayer *logoLayer = [CALayer layer]; 
 logoLayer.frame = NSMakeRect(imageX, imageY, logoWidth, logoHeight);

 [rootLayer addSublayer:logoLayer];

 // Create the layer for the main logo
 CALayer *mainLogo = [CALayer layer];
 mainLogo.frame = NSMakeRect(0, 0, logoWidth, logoHeight);
 mainLogo.contents = logo;

 [logoLayer addSublayer:mainLogo];

 // Create the mirrored layer
 CALayer *logoMirroredLayer = [CALayer layer];
 logoMirroredLayer.frame = NSMakeRect(0, logoHeight + mirroredOffset, logoWidth, logoHeight);

 [logoLayer addSublayer:logoMirroredLayer];

 // Create the layer for the logo
 CALayer *logoBase  = [CALayer layer];
 logoBase.frame = NSMakeRect(0, 0, logoWidth, logoHeight);
 logoBase.contents = logo;

 [logoMirroredLayer addSublayer:logoBase];

 // Create the gradient to make the mirrored effect
 CAGradientLayer *gradientCover = [CAGradientLayer layer];
 gradientCover.frame = NSMakeRect(0, 0, imageRect.size.width, imageRect.size.height);
 gradientCover.colors = [NSArray arrayWithObjects:(id)blackColor, (id)transparentColor, nil];
 gradientCover.locations = [NSArray arrayWithObjects:(id)[NSNumber numberWithFloat:0.0f], (id)[NSNumber numberWithFloat:1.0f], nil];

 [logoMirroredLayer addSublayer:gradientCover];

 // Flip the mirrored layer
 CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity, 180.0f * M_PI / 180 );
 [logoMirroredLayer setAffineTransform:rotateTransform];

 // Make the mirrored layer see through
 logoMirroredLayer.opacity = 0.3f;

 [self setWantsLayer:YES];
}


@end

我不明白的是,在此之前添加了其他子图层,此图层的框架未覆盖徽标的显示位置。

1 个答案:

答案 0 :(得分:0)

你的CALayer面具是什么样的? CIFilter可能会将您的Alpha通道设置为非零,从而遮挡其背后的图层。