noobsmcgoobs
noobsmcgoobs

Reputation: 2746

Draw multiple lines Core Graphics

I have a custom view that will draw animations - just different lines, each line a different color. When user enters information, these lines appear originating at the button and creating lines to different UILabels.

I am following this tutorial techotopia.com core graphics iOS 7 tutorial

It says to make the UIView a custom class and put the drawing inside drawRect: - with your typical creating of context, adding lines, stroke, color, etc. OK fine, but how do I get multiple lines with different colors to be drawn simultaneously?

Do I need to create layers for each line? A new custom class and view for each line?

If I call CGPathCloseSubpathwill that allow me to create a new starting point?

How do I change the color for the new line, and do I need to create a new context for the new line?

Upvotes: 0

Views: 1464

Answers (1)

PowHu
PowHu

Reputation: 2189

Use UIBezierPath is easier than CGPath and CGContext.

-(void)drawRect:(CGRect)rect
{
    UIBezierPath *line1 = [UIBezierPath bezierPath];
    [line1 moveToPoint:CGPointMake(0, 0)];
    [line1 addLineToPoint:CGPointMake(100, 0)];
    [[UIColor redColor] set]; //Set color to red
    [line1 stroke];

    UIBezierPath *line2 = [UIBezierPath bezierPath];
    [line2 moveToPoint:CGPointMake(0, 10)];
    [line2 addLineToPoint:CGPointMake(100, 10)];
    [[UIColor blueColor] set]; //Change color to blue
    [line2 stroke];
}

Upvotes: 1

Related Questions