How to erase Drawing Lines on UIView - iphone

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.

Related

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 draw a arbitrary path with touch move on a UIView

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!

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?

draw line/point on existing UIView without drawRect?

I created line chart. Now I need to display point on this chart when I tap the screen.
What would be the best method? Do I need to call drawRect method again, draw whole chart with marked point?
I'm thinking about something like transparent layer over the chart UIView.
Can I create another transparent UIView and put it on the position of my chart?
Since all drawing is done in a view's drawRect: you can only optimize your chart's drawing so it can be made to update only a part of it and use setNeedsDisplayInRect: (passing the area where the marker should be).
Or you create another UIView subclass that is layered atop of your chart and that does nothing but drawing the markers on a transparent background. Probably easier and faster to implement. It also would have another benefit:
If you make that view only as big as the bounding box of the marker you could also easily animate it, like fading it in and out. Or letting it rotate a little (to see the effect I have in mind, select the "Help" menu in Mac OS X, type something in the search field like "a", and see the marker next to a menu item move a little around a spot).
You can draw a portion of your view using setNeedsDisplayInRect:.

How to avoid background erasing when drawing lines?

I'm writing a paint app.
When drawing a line in CGContext, the background with the size of the bounding box of the line is always got erased - the underlying lines in that box will be cleared.
How to avoid the background erasing?
[EDIT] Better: see How to draw a line on top of an image when finger moves on iPhone. If your image is added as a background specifically, not directly to the view, it will do what you're looking for.
[original] Keep everything you've drawn already, and redraw it in every call to drawRect via setNeedsDisplay.
Or, set your UIView containing the line to UIColor clearColor.