Search code examples
ioslines

Draw multiple lines programatically in iOS


Possible Duplicate:
How To Draw line on touch event?

after lot of browsing, i couldn't find relevant answer yet. I want to draw multiple lines on iOS UIView programmatically during run time i.e allowing user to draw lines by dragging with his finger on the screen. however, the problem is when i draw second line, first drawn line is removed. I want first line to stay in its coordinate and user may draw second straight line by dragging over the screen. Attached is the code:

CGContextRef context    = UIGraphicsGetCurrentContext();                
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);    
CGContextSetLineWidth(context, 2.0);        
CGContextMoveToPoint(context, touchPoint.x,touchPoint.y); 
CGContextAddLineToPoint(context, lastTouchPoint.x, lastTouchPoint.y);
CGContextStrokePath(context);

Solution

  • in every call to drawRect you get passed a dirtyRect which defines an area you have to draw COMPLETELY, old content in that area is GONE. By default, when you have no better logic, you gotta draw everything thats in your view -- ALL lines!

    it isnt an update call, old stuff is not preserved.

    --

    you could add all touches to an array in the order they happen and in drawRect draw every one.