2016-02-22 20 views
0

我有舍入一个30×30的看法:CALayer cornerRadius + masksToBounds 10.11故障?

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

到目前为止,一切都很好:enter image description here

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

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

而且我结束了这,这不是我想要的! enter image description here

这是一个问题,自从我升级到El Capitan后才出现。在优胜美地,掩码为上述代码工作。我错过了什么?

更新:当我设置layer.shouldRasterize = YES;时,不会发生此问题但是我想保留内存,所以我更喜欢另一种解决方案。

回答

0

我发现我自己的解决方案,使用形状层+面膜代替cornerRadius:

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]; 

按预期工作:enter image description here

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