且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iPhone / iPad上画一条线

更新时间:2023-01-18 10:21:11

您将 UIView 并覆盖 - (void)drawRect:(CGRect)rect 方法。

获取图形上下文:

  CGContextRef context = UIGraphicsGetCurrentContext(); 

你可以使用它来进行Core Graphics调用,例如:

  CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextBeginPath(context); (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 Programming Guide了解所有细节。


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).

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).

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?

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

You subclass your UIView and override the - (void)drawRect:(CGRect)rect method.

In there you grab a graphics context:

CGContextRef context = UIGraphicsGetCurrentContext();

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);

Look up the Quartz 2D Programming Guide for all the details.