xcode iOS:使用 CGContextAddLineToPoint 绘制多条线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12458531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS:Draw Multiple Lines Using CGContextAddLineToPoint
提问by Dinesh Kaushik
I am writing this code in drawrect
我正在用 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
但是这段代码总是只画两条不正确的线它从不画第三行当我给出静态值而不是 x 和 y 代码在三行中工作正常当我 NSLOG x 和 y 我得到正确的期望值但线没有根据为此,我想绘制 4-5 条不断变化的坐标线
Please tell me where I am going wrong Or any other alternative to solve this problem
请告诉我哪里出错了或解决此问题的任何其他替代方法
回答by Ganee....
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...
上面的代码工作正常,他们正在制作三行单独的行(所有(502、530)的起点都相同)。我认为你给两个点(如 x3,y3 或 x4,y4)的结束位置是相同的方向,这就是为什么你只得到两条线,如果你想区分这些线,你可以用不同的颜色制作线...
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);