CALayer 在内容图像顶部绘制阴影
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将偏移量的 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."