添加CAShapeLayer作为子图层不可见

时间:2014-02-06 16:23:11

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

我第一次体验CALayers,毫无疑问,他做的事非常简单,非常错误。

以下代码位于从UIView扩展的类中。我正在访问视图的CALayer,这很好,并且尝试根据用户触摸绘制的线来附加子图层。 (CGPath

通过将背景设置为亮橙色,我可以看到我将右CALayer传递到我的函数中,但如果我尝试将CAShapeLayer *line设置为橙色 - 我似乎没有能够在屏幕上看到它。

我想象我把两者连在一起是错误的,或者错过了定义某种尺寸或者某种东西,但是我想要尝试的东西是新的。谁能让我直截了当?

(void)makeLineLayer:(CALayer *)layer{
    CAShapeLayer *line = [CAShapeLayer layer];
    //This line sets the view to orange - so I know 'layer' is pointing to the right thing
    //layer.backgroundColor = [UIColor orangeColor].CGColor;
    UIBezierPath *linePath=[UIBezierPath bezierPath];
    [linePath moveToPoint:self.lineStart];
    [linePath addLineToPoint:self.lineEnd];
    [linePath setLineWidth:60.0];

    line.path=linePath.CGPath;
    //This below line is meant to show me where my sublayer is - I currently can't see it.
    line.fillColor = [UIColor orangeColor].CGColor;
    line.opacity = 1.0;
    [layer addSublayer:line];
}

1 个答案:

答案 0 :(得分:4)

您的代码没有任何问题。

但是有一个问题突出 - 没有什么可以“填补”,因为你只有一点可以转移。

设置lineWidth在这里没有用,因为行的path只会将其视为一个点与另一个点形成的一行,而不考虑width的{​​{1}} 1}}。

如果您希望看到您的代码确实有效,请添加此代码并查看它:

UIBezierPath

玩得开心。希望这会有所帮助。

附录 ,如果您只想显示该行和一行,请添加以下内容:

UIBezierPath *linePath=[UIBezierPath bezierPath];
[linePath moveToPoint:self.lineStart];
[linePath addLineToPoint:self.lineEnd];

[linePath addLineToPoint:(CGPoint){250.0f, 350.0f}]; <<=== random points I chose.

[linePath setLineWidth:60.0];

然后你可以删除我提到的那个:

line.strokeColor = [UIColor <YOUR_COLOR>].CGColor;