CALayer cornerRadius + masksToBounds 10.11 故障?

CALayer cornerRadius + masksToBounds 10.11 glitch?

我有一个 30x30 的圆形视图:

CALayer * layer = self.layer;
layer.backgroundColor = [NSColor redColor].CGColor;
layer.cornerRadius = 10.0f;
layer.masksToBounds = YES;

到目前为止,还不错:

然后我添加一个子层,像这样:

CALayer * subLayer = [CALayer layer];
subLayer.backgroundColor = [NSColor yellowColor].CGColor;
subLayer.frame = CGRectMake(0.0f, 0.0f, 10.0f, 10.0f);
[layer addSublayer:subLayer];

最后我得到了这个,这不是我想要的!

这是我升级到 El Capitan 后才出现的问题。在 Yosemite 中,屏蔽适用于上述代码。我错过了什么?

更新:当我设置 layer.shouldRasterize = YES; 时不会出现此问题,但是我想减少内存,所以我更喜欢其他解决方案。

我找到了自己的解决方案,使用形状层 + 遮罩代替角半径:

CALayer * layer = self.layer;
layer.backgroundColor = [NSColor redColor].CGColor;
//
// code to replace layer.cornerRadius:
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
float const r = 10.0f;
float const w = self.bounds.size.width;
float const h = self.bounds.size.height;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, r, 0.0f);
CGPathAddArcToPoint(path, NULL, w, 0.0f, w, r, r);
CGPathAddArcToPoint(path, NULL, w, h, w - r, h, r);
CGPathAddArcToPoint(path, NULL, 0.0f, h, 0.0f, h - r, r);
CGPathAddArcToPoint(path, NULL, 0.0f, 0.0f, r, 0.0f, r);
CGPathCloseSubpath ( path );
shapeLayer.path = path;
CGPathRelease(path);
self.layer.mask = shapeLayer;
//
// add the sublayer
CALayer * subLayer = [CALayer layer];
subLayer.backgroundColor = [NSColor yellowColor].CGColor;
subLayer.frame = CGRectMake(0.0f, 0.0f, 10.0f, 10.0f);
[layer addSublayer:subLayer];

按预期工作:

(当然,如果有人有更优雅的修复,我很乐意看到它!)