Is drawRect() meant to simply be a hook in which to execute your drawing code?
It is meant to redraw the region (rect) that is passed to you, using the current graphics context stack. No more.
Or is this method supposed to also redraw regions that are "damaged", and need repainting?
No. If dirty regions do not overlap, you may receive multiple invocations of drawRect:
with different rects passed to you. Rects are invalidated using setNeedsDisplayInRect:
. If only a portion of your view's surface must be redrawn, then you will be requested to draw that portion when it is time to draw.
Can I just draw my stuff once and then it "sticks", or must I repaint the whole scene at any time via drawRect()?
It does not 'stick'. Rects become invalidated during your app's execution, and you are requested to redraw those rects when the view system needs to update the screen. You repaint only the rect that is requested.
In some cases, a simple implementation may (rather lazily) invalidate the view's entire rect whenever a portion is invalidated. That's typically bad because it typically requires more drawing than is necessary, and is particularly wasteful when views are not opaque.
Java's Graphics2D object works this way- you must draw your whole "image" every time paint() is called, so you must be prepared to re-construct it at any time (or cache it).
Not so with AppKit or UIKit.
How would you implement a simple drawing program? Would you have to "remember" each line/point/stroke that the user drew, and replicate that each time drawRect() is called?
You will have to remember the context (e.g. each line/point/stroke) required to draw your view. You only need to draw the region that is requested. Technically, the graphics system would not complain if you were to draw outside that rect, but that could lead to artifacts.
For complex rendering, it may be easier or more efficient to draw to an external buffer (e.g. bitmap), then to use that prerendered bitmap representation to get things onscreen while in drawrect:
. (see also Brad's answer for layers)
How about "offscreen" rendering; can you do all your drawing and then call [self setNeedsDisplay] to have your writes flushed to the screen?
Yes, you can do that. Specifically, you would render to an external buffer (e.g. a bitmap), when you are finished rendering to the bitmap, invalidate the rect you wish to draw, then draw to the screen using the data in the bitmap when drawRect:
is called.
Let's say that in response to a user's touch, I want to put an "X" on the screen where he touched up. The X should remain there, and each new touch produces another X. Do I need to remember all these touchup coordinates and then draw them all in drawRect() ?
Well, you have a few options. Your proposal is one (assuming you draw within the rect passed to you). Another would be to create an 'X' view, and simply remember the points needed to reconstruct the view if you need those Xs to persist across launches. In many cases, you can easily divide complex problems into layers (simple 2D game):
- 1) The background image with the horizon.
- 2) Some things in the foreground that don't change frequently.
- 3) The character the player the user uses to navigate through the game.
So, most problems can be easily divided so you don't have to render everything all the time. This reduces complexity and improves performance if done well. If done poorly, it can be several much worse.