开发者

CALayer draws shadow on top of content image

开发者 https://www.devze.com 2023-02-13 18:13 出处:网络
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.

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:

CALayer draws shadow on top of content image

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.


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."

0

精彩评论

暂无评论...
验证码 换一张
取 消