无法将形状图层添加到视图的图层中

时间:2013-09-09 20:21:54

标签: ios objective-c cocoa-touch calayer cashapelayer

我正在使用CAShapeLayer创建一个图层CAShapeLayer,如下所示:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = CGRectMake(150, 50, 200, 200);
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
[self.view.layer addSublayer:shapeLayer];

然而,当我执行代码时,我无法在模拟器/设备中看到我的shapeLayer 我在这里失踪了什么。

PS:如果我使用的是CALayer *shapeLayer = [CALayer layer];,那么它就可以工作。

1 个答案:

答案 0 :(得分:4)

添加一个路径,例如

 shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) 
                                              cornerRadius:radius].CGPath;

成型层需要形状......


我把它放在我的View Controller中,它运行正常:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(150, 50, 200, 200);
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    shapeLayer.strokeColor = [UIColor orangeColor].CGColor;

    NSUInteger radius = 90;
    shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
                                                 cornerRadius:radius].CGPath;
    [self.view.layer addSublayer:shapeLayer];   
}

enter image description here

如果您更改路径

shapeLayer.path = [UIBezierPath bezierPathWithRect:shapeLayer.bounds].CGPath;

它会导致

enter image description here