gpt4 book ai didi

cocoa - CALayer 过滤器覆盖其他子层

转载 作者:行者123 更新时间:2023-12-03 17:26:02 26 4
gpt4 key购买 nike

我正在开发一个信息亭类型的应用程序。我想在中间有一个 Logo (可行)。现在我尝试在 Logo 周围添加一些 float 层。我可以绘制图层(在 drawDots 中),但是当我向其添加过滤器时 - 它会覆盖我绘制的 Logo 图层。我已经在这里复制了所有重要的代码。同样,如果我注释掉在 dotLayer 上设置过滤器数组的行,机器人点层的紫色方 block 和 Logo 是可见的,但是当我应用过滤器时 - Logo 不可见。我不太明白,因为我正在为这些设置正确的框架。我还尝试在 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

我不明白的是,其他子层都添加在此层之前,并且该层的框架没有覆盖 Logo 显示的位置。

最佳答案

您的 CALayer 蒙版是什么样子的? CIFilter 可能会将您的 Alpha channel 设置为非零,从而遮挡其后面的图层。

关于cocoa - CALayer 过滤器覆盖其他子层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com