iOS:CAShapeLayer,更改外层的颜色

时间:2015-06-01 11:47:13

标签: ios objective-c cashapelayer

我目前正在使用CAShapeLayer来满足我的要求。我成功地实现了fillcolorstrokecolor等...但我想改变那个CAShapeLayer的外部部分的颜色。我尝试用backgroundcolor和&来自SO的许多答案,但无法做到。任何人都可以指导我解决方案。附加屏幕截图Current Screen Change White color to black color with alpha 0.6

编辑1:创建图层的代码

// create layer mask for map
CAShapeLayer *maskLayer = [CAShapeLayer layer];
mapView.layer.mask = maskLayer;
//    maskLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] CGColor];
self.maskLayer = maskLayer;

// create shape layer for circle we'll draw on top of map (the boundary of the circle)
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.lineWidth = 3.0;
circleLayer.fillColor = [[UIColor clearColor] CGColor];
circleLayer.strokeColor = [[UIColor blackColor] CGColor];

//    circleLayer.borderColor = [[UIColor blackColor] CGColor];
//    circleLayer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] CGColor];

[mapView.layer addSublayer:circleLayer];
self.circleLayer = circleLayer;

编辑2:Xcode的Viewer调试器 Xcode debugger

在上图中,我可以看到我已将背景颜色设置为self.view。但我希望它超越我的地图视图&颜色应该是半透明的,这样圆形外的地图数据也应该是可见的。

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用XCode视图的调试器来了解此白色背景在视图层次结构中的位置?

您将视图的backgroundColor用作圆圈。所以我的猜测是这个白色背景必须在你的视图外面 - 你的面具被剪掉了 - 所以你应该尝试改变你的viewController的视图Stackoverflow wants me to put some code here because of jsfiddle, but I really don't know if it is good idea to paste here allt that long JS code (或者在你的自定义圆形视图上方的任何其他视图)

答案 1 :(得分:0)

[self.view.layer setBackgroundColor:[UIColor grayColor].CGColor];

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:self.view.center
                                                      radius:45.0
                                                  startAngle:0
                                                    endAngle:2.0*M_PI
                                                   clockwise:YES];

CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.bounds = CGRectMake(0, 0, 2.0*(45.0), 2.0*(45.0));
circleLayer.path   = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.fillColor = [UIColor whiteColor].CGColor;
circleLayer.lineWidth   = 3.0; // your line width

[self.view.layer addSublayer:circleLayer];

看看它对你有帮助。