Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone clear CGContext

I create a circle with a nice shadow with this code (I use MonoTouch.net for iPhone, Objective-C answers are fine of course)

        UIGraphics.PushContext (ctx);
        SizeF shadowSize = new SizeF (0f, -3f);
        ctx.SetRGBFillColor (194f / 255f, 212f / 255f, 238f / 255f, 1f);
        ctx.SetAllowsAntialiasing (true);
        ctx.SetShadowWithColor (shadowSize, 20, new CGColor (0.9f, 0.7f));
        RectangleF area = new RectangleF (35f, 15f, 210f, 210f);
        ctx.FillEllipseInRect (area);
        UIGraphics.PopContext ();

Then I want to add to it an arc and lines. When I do, the colors and shadow etc seem to stick around? How do I 'start fresh' while drawing my UIView ? (It's all in the same UIView; I am building up the image)

like image 231
Ian Vink Avatar asked Jan 19 '10 00:01

Ian Vink


2 Answers

If you mean to clear everything that's drawn so you have a blank canvas, try CGContextClearRect before drawing anything.

But I think you mean that you want the shadow, fill color, etc. to only apply to that ellipse and not to the things that you draw afterward. To do that, you want to call the same state-setting methods again, but with different arguments. For instance, CGContextSetShadowWithColor expects a shadow color. But if you pass NULL for that argument, it will disable shadowing.

See also the CGContextSetShadow documentation, which has a note about all the ways you can disable shadowing. Pick the best one for you.

I think your main problem is that you're not taking advantage of Apple's excellent documentation. I gather that MonoTouch.net essentially maps Objective-C APIs to .NET modules with similar or identical symbol names. So with a quick Google search, you should be able to find the corresponding documentation in the iPhone OS Reference Library.

like image 82
easeout Avatar answered Oct 19 '22 19:10

easeout


Before we begin drawing, save the state:

CGContextSaveGState(ctx);

Then after we are finished, return the state to what it was at the beginning:

CGContextRestoreGState(ctx);
like image 2
Ian Vink Avatar answered Oct 19 '22 19:10

Ian Vink