Reputation: 1206
I am trying to draw to lines each with a different color. (but connected) If I don't add and stroke the second line the color is red, but the moment I add the second line the whole thing is yellow. How can I have two lines show with different colors? my code is below.
CGPoint FirstCGPoint = [self pointForMapPoint:cr.points[0]];
CGPathMoveToPoint(path, NULL, FirstCGPoint.x, FirstCGPoint.y);
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGPoint SecondCGPoint = [self pointForMapPoint:cr.points[1]];
CGPathAddLineToPoint(path, NULL, SecondCGPoint.x, SecondCGPoint.y);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, [[UIColor yellowColor] CGColor]);
CGPoint NewFirstCGPoint = [self pointForMapPoint:cr.points[3]];
CGPathAddLineToPoint(path, NULL, NewFirstCGPoint.x, NewFirstCGPoint.y);
CGContextAddPath(context, path);
CGContextStrokePath(context);
Upvotes: 1
Views: 1880
Reputation: 966
check one time below code.. may this help you
First of all take global array and add different UIColor to it as per your requirement
Add this code to viewDIdLoad
array=[[NSArray alloc]initWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor yellowColor], nil];
take another global variable for int
type try to set it value by incriment or decrement or assign a value to it
then use the code like this in place of CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
like this..
UIColor *colo=(UIColor*)[array objectAtIndex:1];
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context,
colo.CGColor);
NOTE: in array i had used int.1 in at objectAtIndex in array.At that place put you selected index which is incremented or decremented or assigned index to it
if you wand to set different red,green and blue and saturation values. Then in .h declare
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat opacity;
and in
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 10.0;
opacity = 1.0;
[super viewDidLoad];
}
for different color set the different float value as per your selection color and use like this in context
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
May this code help you .Happy coding
Upvotes: 1
Reputation: 6952
Try it, you needn't to use path.
CGPoint FirstCGPoint = CGPointMake(10.0f, 40.0f) ;
CGContextMoveToPoint(context, FirstCGPoint.x, FirstCGPoint.y) ;
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGPoint SecondCGPoint = CGPointMake(10.0f, 100.0f) ;
CGContextAddLineToPoint(context, SecondCGPoint.x, SecondCGPoint.y);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, [[UIColor yellowColor] CGColor]);
CGPoint NewFirstCGPoint = CGPointMake(100.0f, 100.0f) ;
CGContextMoveToPoint(context, SecondCGPoint.x, SecondCGPoint.y) ;
CGContextAddLineToPoint(context, NewFirstCGPoint.x, NewFirstCGPoint.y) ;
CGContextStrokePath(context);
Upvotes: 2
Reputation: 540075
For the second line, you have to start a new path (from the second to the third point).
If you just append a new segment to the first path, the entire path will be stroked in the new color (yellow).
Upvotes: 1