How to draw a arbitrary path with touch move on a UIView - iphone

I know that you can draw a line easy by Core Graphic on UIView in a drawrect function. However, I only can draw a straight line。 If I want to draw a arbitrary path with the touch move, what should I do? Can someone give a hint?
Best Regards,

You could use the CGPoints that are being passed to touchesMoved: and draw a line in between each on of those CGPoints in order to draw your arbitrary path. You would only need to draw a new line once, unless you are redrawing your screen, which means you would have to store all the points in an NSMutableArray, and redraw them every time. Hope that Helps!

Related

Drawing to the screen

I just want to draw point and line as per finger touch and moved in iphone screen.
What is the best way to draw point in iphone?
Can anyboday tell me how to draw point as per finger touch and moved in iphone ?
Thanks,
You can't "just draw" on the screen. Closest approximation might be to save some coordinates from some view's touch delegate methods and call setNeedsDisplay. When the view's drawRect is called, you can then use some Core Graphics (CG) drawing commands with those saved coordinates into the given drawing context. The OS will shortly then composite that view context onto the display screen.
check with this tutorial,i am sure it will definetly help you.
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing/
you can draw line on Image or on UIView using UIBezierPath class.. in this class object you can store the user touch points and draw line using this points array..
for more details see the tutorial and demo from this link..
Drawing on View and Image

Continuous drawing in CGContext with drawRect

My question might be answered somewhere but I just couldn't find solution after a long research.
I need to draw visually shapes (curves, lines, rectangles etc.) on iPhone one on top of the previous. I did that using CGContext to draw over an image and it's working fine. However in my app the drawing view resizes on device rotation and the lines become blurred because of the different image size.
That's why I subclassed UIView and to call setNeedsDisplay from touchesMoved and touchesEnded. In drawRect I'm passing the point and... almost everything works OK.
However I have two problems:
1. Every time drawRect is called it clears previous drawing and starts over so I can't add a new shape.
2. Second is followed by same thing - I can't make a curve as on every move drawRect is called and the previous point is dismissed and a line is added from the starting point to the current.
So am I doing the whole thing wrong and is there some other better approach to this.
Thanks in advance!

How to erase Drawing Lines on UIView

I drew a line on UIView. It's working properly. I did the same thing as described here : [question]:http://stackoverflow.com/questions/2595446/drawing-an-image-is-completely-out
And now I need to erase those lines. Erase means after I move my finger on the touch screen I need to erase it. Just like an eraser. How can I do it?
If you are asking to erase the whole line when the user touches it, than i don't know how you can achieve that, but if you are asking to erase the part of the line were the user touches, than you can draw a line of the color the view that you are using to draw the line on to in that way you will get the effect of an eraser.
You have to call setNeedsDisplayInRect: (UIView) to set the rectangle of the line as invalid. Only this rectangle will be redrawn until the next drawing cycle.
Make sure you have a flag in the drawRect: method you can ask for, that identifies whether the line should be drawn or not.

Ellipse through TouchMoved?

is it possible to draw Ellipse through UITouchMoved in iphone sdk as we do in MSPaint?Any
example please?
Just save the rect in the touch moved delegate, and draw an ellipse in the rect (maybe using CGContextStrokeEllipseInRect) whenever the view's drawRect gets called. Call setNeedsDisplay to say that the drawRect needs to be called sometime before returning to the run loop.
Guessing that you mean ellipse rather than eclipse? If so, use +[UIBezierPath bezierPathWithOvalInRect:].

Draw a straight line and have it refresh if UIImageView is moved

How can I draw a straight line between two UIImageViews and have the line "refresh" if one of the views is moved. I assume I'll have to use NSNotificationCenter, but other then that I'm a bit stuck.
Thanks for your help!!!
A good way to do this would be to use KVO (Key-Value Obverving).
If you use KVO to watch for changes in the bounds (or frame) of the views, you will get notified about position changes, at which point you can (re-)render a line on the display.
More about KVO: https://stackoverflow.com/questions/1470167/is-there-any-tutorial-out-there-on-key-value-coding-and-key-value-observing
As for rendering a line: one often used technique is to write a subclass of UIView in which you override the drawRect method and draw a line using Core Graphics. See this question: How do I draw a line on the iPhone?