使用CGContext画线
我想使用 CGContext 画一条线,到目前为止我所拥有的是:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);
它总是从左上角绘制到右上下角。如何调整这条线的起点和终点?如何调整线的长度?
I wanted to draw a line using CGContext and what I have so far is:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);
It always draws from the top left corner to the top bottom right corner. How can I adjust the start and end origin of this line? How do I adjust the length of the line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CoreGraphics == 美好时光。
我已经有一段时间没有徒手做任何事情了,这些天我所做的就是在绘图操作之前构建一切。请记住,从 Logo 时代起,就有一个隐式“光标”,并且您可以移动它而不使移动成为绘图操作,但您必须指定它。我认为一个好的方法(至少对于静态图形)是创建那些必须首先绘制的路径,然后一遍又一遍地使用该路径来进行填充、描边、着色等操作。
等等。如果需要,您可以在最后释放路径,或者保留引用以供以后使用。
CoreGraphics == Good Times.
It's been a while since I did anything freehand as it were, what I do these days is to build everything ahead of drawing operations. Remember that there is an implicit 'cursor', from the days of Logo, and you can move it without making the move a drawing operation, but you have to specify it. I think a good approach (at least for static figures) is to create those paths you will have to draw first, then using the path over and over for things like fill, stroke, shading.
Etc. You can release the path at the end if you want, or keep the reference around for use later on.
这两条线负责起点和终点:
通过调整这两个 x、y 点,您可以调整线。线的长度取决于起点和终点。
继 psoft 的回答之后 - 这是一个基本的绘图示例项目,包括创建路径并抚摸它。
Quartz 2D 指南中的更多示例代码对此进行了更详细的解释 路径。
These two lines are responsible for the start and end points:
By adjusting these two x, y points you can adjust the line. The length of the line depends on the start and end points.
Following on from psoft's answer - here's a basic example project of drawing, including creating a path and stroking it.
This is explained in more detail with more sample code in the Quartz 2D guide for paths.