我正在用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);但这段代码总是只画两条线,这是不正确的。当我给出静态值而不是x和y时,它永远不会画第三条线。当我不给x和y时,代码对三条线工作得很好。我得到了适当的期望值,但没有根据我想要画的4-5条线的坐标连续变化来绘制线。
请告诉我我哪里出错了,或者有没有其他办法来解决这个问题
发布于 2012-09-17 20:54:31

上面的代码运行得很好,他们生成了三行(起始点都是相同的(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);https://stackoverflow.com/questions/12458531
复制相似问题