Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a line in iPhone / iPad

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

like image 572
Paul Peelen Avatar asked Jan 24 '11 14:01

Paul Peelen


People also ask

Is there a drawing tool on iPhone?

Use the Notes app to draw a sketch or jot a handwritten note with your finger. You can choose from a variety of Markup tools and colors and draw straight lines with the ruler.


1 Answers

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.

like image 95
Silromen Avatar answered Oct 03 '22 02:10

Silromen