views:

260

answers:

2

Is there a way to draw on the iPhone screen (on a UIView in a UIWindow) outside of that view's drawRect() method? If so, how do I obtain the graphics context?

The graphics guide mentions class NSGraphicsContext, but the relevant chapter seems like a blind copy/paste from Mac OS X docs, and there's no such class in iPhone SDK.

EDIT: I'm trying to modify the contents of the view in a touch event handler - highlight the touched visual element. In Windows, I'd use GetDC()/ReleaseDC() rather than the full cycle of InvalidateRect()/WM_PAINT. Trying to do the same here. Arranging the active (touchable) elements as subviews is a huge performance penalty, since there are ~hundred of them.

A: 

I dont think ull be able to draw outside drawRect...but to get the current graphic context all you do is CGContextRef c = UIGraphicsGetCurrentContext(); hope that helps.

Daniel
UIGraphicsGetCurrentContext() returns nil if called outside of drawRect().
Seva Alekseyev
really? good to know...anyway if u want to redraw u can just call setNeedsDisplay on your view, that will call drawRect again and u can draw whatever u need
Daniel
I'm specifically asking if an alternative exists. I will take a "No" for an answer :)
Seva Alekseyev
ok no u wont be able to, drawRect is where drawing takes place for a UIView
Daniel
+1  A: 

No. Drawing is drawRect:'s (or a CALayer's) job. Even if you could draw elsewhere, it would be a code smell (as it is on the Mac). Any other code should simply update your model state, then set yourself as needing display.

When you need display, moving the display code elsewhere isn't going to make it go any faster. When you don't need display (and so haven't been set as needing display), the display code won't run if it's in drawRect:.

I'm trying to modify the contents of the view in a touch event handler - highlight the touched visual element. In Windows, I'd use [Windows code]. … Arranging the active (touchable) elements as subviews is a huge performance penalty, since there are ~hundred of them.

It sounds like Core Animation might be more appropriate for this.

Peter Hosey