我有一个 UIView 子类,它实例化其层的三个同级子层。其中两个子图层的内容设置为图像文件;第三个有一个图形绘制在
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
方法。问题是我的绘图层总是出现在图像层的后面。我尝试重新排序子图层数组并更改子图层的 zPosition 属性无济于事。我错过了什么?
- (void)setupLayers
{
CALayer *rootLayer = [self layer];
_meterLayer = [CALayer layer];
[_meterLayer setContents:(id)[[UIImage imageNamed:@"HealthMeterRadial60x60.png"] CGImage]];
_pointerLayer = [CALayer layer];
[_pointerLayer setContents:(id)[[UIImage imageNamed:@"HealthMeterRadialPointer60x60.png"] CGImage]];
_labelLayer = [CALayer layer];
[self set_labelText:@"Health"];
[rootLayer addSublayer:_meterLayer];
[rootLayer insertSublayer:_pointerLayer above:_meterLayer];
[rootLayer insertSublayer:_labelLayer above:_pointerLayer];
}
和 drawLayerInContext 方法:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if( layer = _labelLayer) {
//draw graphics ... omitted for clarity; works, but always behind images
}
}