CALayer 在内容图像顶部绘制阴影

发布于 2024-10-19 08:54:27 字数 930 浏览 1 评论 0原文

我有一个 CALayer 实例,我已将其设置为 png 文件作为内容。设置各种阴影属性后,阴影将绘制在图像上方/顶部,而不是下方。

代码如下:

CALayer *boardLayer = [CALayer layer];
boardLayer.frame = CGRectMake(self.layer.frame.origin.x, 
  self.layer.frame.origin.y, self.layer.frame.size.width, BOARD_HEIGHT);

boardLayer.shadowColor = CGColorCreateGenericRGB(100.0, 100.0, 0.0, 1.0);
boardLayer.shadowOffset = CGSizeMake(10, 10);
boardLayer.shadowOpacity = 0.99f;
boardLayer.shadowRadius = 5.0f;
boardLayer.contents = (id)[self imageToImageRef:[NSImage imageNamed:@"board.png"] 
                                   withMaxWidth: 0
                                   andMaxHeight: 0];
boardLayer.contentsGravity = kCAGravityResize;

boardLayer.masksToBounds = NO;

在屏幕截图中,您可以看到黄色阴影位于内容上方:

在此处输入图像描述

有什么东西吗我缺少设置 CALayer 的过程吗?

谢谢,马克。

编辑:我还尝试了使用负 Y 偏移的上述代码,结果相同,阴影位于图像上方。

I have a CALayer instance for which I've set a png file as content. After setting the various shadow properties, the shadow is drawn above/on top of the image and not below it.

Here's the code:

CALayer *boardLayer = [CALayer layer];
boardLayer.frame = CGRectMake(self.layer.frame.origin.x, 
  self.layer.frame.origin.y, self.layer.frame.size.width, BOARD_HEIGHT);

boardLayer.shadowColor = CGColorCreateGenericRGB(100.0, 100.0, 0.0, 1.0);
boardLayer.shadowOffset = CGSizeMake(10, 10);
boardLayer.shadowOpacity = 0.99f;
boardLayer.shadowRadius = 5.0f;
boardLayer.contents = (id)[self imageToImageRef:[NSImage imageNamed:@"board.png"] 
                                   withMaxWidth: 0
                                   andMaxHeight: 0];
boardLayer.contentsGravity = kCAGravityResize;

boardLayer.masksToBounds = NO;

In the screenshot you can see that the yellow shadow is above the content:

enter image description here

Is there something I'm missing in setting up the CALayer?

Thanks, Mark.

EDIT: I also tried the above code with a negative Y offset, same result, the shadow is above the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-10-26 08:54:27

尝试将偏移量的 Y 设置为负数 (10, -10)。您没有指出这是 Mac 还是 iPhone,但您可能会对 UIKit、AppKit、CoreGraphics 和 CoreAnimation 的不同部分具有不同的坐标系这一事实感到惊讶。确保您知道哪条路是“向上”。

Try setting your offset's Y to be negative (10, -10). You don't indicate if this is Mac or iPhone, but you may be getting surprised by the fact that different parts of UIKit, AppKit, CoreGraphics and CoreAnimation have different coordinate systems. Make sure you know which way is "up."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文