Reputation: 2987
I am writing this code in drawrect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextMoveToPoint(context, 502,530);
CGContextAddLineToPoint(context, x2, y2);
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x3, y3);
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x4, y4);
NSLog(@"%d,%d--%d,%d--%d,%d",x2,y2,x3,y3,x4,y4);
CGContextStrokePath(context);
But this code always draw only two lines which are incorrect It never draws the third line When i give static values instead of x and y code works fine with three lines When I NSLOG x and y I get proper desired values but lines are not drawn according to that I want to draw 4-5 lines with continuesly changing coordinates
Please tell me where I am going wrong Or any other alternative to solve this problem
Upvotes: 2
Views: 7463
Reputation: 302
The above code is working fine, they are making three individual lines (the starting point is same for all(502, 530)). I think you are giving ending position for two points (like x3,y3 or x4,y4) are same directions, that is why you are getting two lines only, if you want to differentiate those lines you can make the lines in different color like...
int x2, y2, x3, y3,x4, y4;
x2 = 100;
y2 = 100;
x3 = 200;
y3 = 200;
x4 = 200;
y4 = 50;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
// line 1
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextMoveToPoint(context, 502,530);
CGContextAddLineToPoint(context, x2, y2);
CGContextStrokePath(context);
// line 2
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x3, y3);
CGContextStrokePath(context);
// line 3
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 502, 530);
CGContextAddLineToPoint(context, x4, y4);
CGContextStrokePath(context);
NSLog(@"%d,%d--%d,%d--%d,%d",x2,y2,x3,y3,x4,y4);
Upvotes: 2