首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:使用CGContextAddLineToPoint绘制多条直线

iOS:使用CGContextAddLineToPoint绘制多条直线
EN

Stack Overflow用户
提问于 2012-09-17 19:46:28
回答 1查看 7.3K关注 0票数 1

我正在用drawrect编写这段代码

代码语言:javascript
复制
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条线的坐标连续变化来绘制线。

请告诉我我哪里出错了,或者有没有其他办法来解决这个问题

EN

回答 1

Stack Overflow用户

发布于 2012-09-17 20:54:31

上面的代码运行得很好,他们生成了三行(起始点都是相同的(502,530))。我认为你给出了两个点(如x3,y3或x4,y4)的结束位置是相同的方向,这就是为什么你只得到两条线,如果你想区分这两条线,你可以用不同颜色的线,比如...

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12458531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档