2016-02-22 45 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

(當然,如果任何人有一個更優雅的修復,我很樂意看到它!)