更改选定的CAShapeLayer的填充颜色

9

当包含它的图层被点击时,我想改变CAShapeLayer的填充颜色。 我可以通过以下方式更改所选图层的背景颜色:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CALayer *layer = [(CALayer *)self.view.layer.presentationLayer hitTest:point];
layer = layer.modelLayer;
layer.backgroundColor = [UIColor blueColor].CGColor;
}

这将像预期的那样将“layer”的背景变为蓝色。 我的问题是如何更改“layer”内部CAShapelayer的颜色? 谢谢!

1个回答

17

CAShapeLayer具有属性fillColor,您可以传递CGColor来更改:

CAShapeLayer* shapeLayer = (CAShapeLayer*)layer.modelLayer;
shapeLayer.fillColor = [UIColor blueColor].CGColor;

另请参阅:CAShapeLayer类参考


没错。但是.fillColor属性应该在主线程上运行。如果这个语句没有被执行,请考虑一下这个语句是否在主线程上运行。 - Tuan Nguyen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接