在 iPhone/iPad 中画线 [英] Drawing a line in iPhone / iPad
问题描述
当用户可以画线时,我想开发一个应用程序......但我不想画直线,而是想在用户画线时显示线.当用户从 A 点到 B 点时,我想拉直这条线(如果用户想要的话).
I would like to develop an app when the user can draw lines... but I do not want to draw straight lines but want to show the line as the users draws it. When the user gets from point A to B I would like to straighten the line (if the users wants this).
为了能够做到这一点,我想将视图更改为从 0,0(左上角)开始到 320,480(对于 iPhone)和 768,1024(对于 iPad)(右下角)结束的网格.
To be able to do this I want to change my view into a grid starting at 0,0 (top left) and ending at 320,480 (for iPhone) and 768,1024 (for iPad) (bottom right).
对于这个问题,我的 A 点位于 10,10,B 点位于 100,100.
For this question I have point A at 10,10 and point B at 100,100.
我的问题:
- 如何创建这个网格?
- 如何创建这些点?
- 如何在不拉直的情况下绘制这条线?
- 如何画直线?
My question:
- How do I create this grid?
- How do I create these points?
- How do I draw this line without straightening it?
- How do I draw the straighten line?
我的问题是我熟悉创建普通"用户界面应用程序.我不熟悉 Open-GL 等.
My problem is that I am familiar with creating "normal" UI apps. I am not familiar with Open-GL ect.
我希望有人可以帮助我.
I hope someone can help me with this.
最好的问候,
保罗·皮伦
Best regards,
Paul Peelen
推荐答案
你继承你的 UIView
并覆盖 - (void)drawRect:(CGRect)rect
方法.
You subclass your UIView
and override the - (void)drawRect:(CGRect)rect
method.
您可以在其中获取图形上下文:
In there you grab a graphics context:
CGContextRef context = UIGraphicsGetCurrentContext();
然后你用它来调用 Core Graphics,比如:
And you use that to make Core Graphics calls, like:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
for (k = 0; k < count; k += 2) {
CGContextMoveToPoint(context, s[k].x, s[k].y);
CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y);
}
CGContextStrokePath(context);
查看 Quartz 2D 编程指南了解所有详细信息.
Look up the Quartz 2D Programming Guide for all the details.
这篇关于在 iPhone/iPad 中画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!