Question

I'm having my first experience with CALayers, and am no doubt doing something very simple, very wrong.

The below code is in a class extended from a UIView. I am accessing the CALayer of the view, which is fine, and the attempting to attach a sublayer, based on a line drawn by the users touch. (The CGPath)

By setting the background to bright orange, I can see that I'm passing the right CALayer into my function, yet if I try and set the CAShapeLayer *line to orange - I don't seem to be able to see it on the screen.

I'm imagining I've connected the two together wrong, or missed defining some size or something, but am fresh out of things to try. Can anyone set me straight?

(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];
}
Was it helpful?

Solution

There is nothing wrong with your code there.

But one problem stands out - there is nothing to "fill" given that you only have a point to move to.

Setting lineWidth would not help here because the path of line only sees it as a line that is formed one a point to another, not taking account the width of the UIBezierPath.

If you would like to see that your code does in fact works, add this and see that it does:

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];

Have fun. Hope this helps.

Addendum, if all you want is to show the line and a line only, please add the following:

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

And you can then remove this I mentioned:

[linePath addLineToPoint:(CGPoint){250.0f, 350.0f}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top