2

I have a TestView inherited from UIView, and everything is drawn on the screen using its drawRect method.

But if I don't want drawRect to clear the view before drawing, I used

self.clearsContextBeforeDrawing = NO;  
self.opaque = NO;   // also added because the doc says the rect 
                    //   passed to drawRect will be filled with 
                    //   background color first if it is set to YES

in the initWithFrame method.

The drawRect method is invoked by using

[self.view setNeedsDisplay];

in the ViewController event handler. (for touchesMoved events)

But still, everything is cleared before anything is drawn? How to make it work?

Jeremy L
  • 3,770
  • 6
  • 41
  • 62

1 Answers1

3

I think this answers your question. From the answer:

You cannot prevent the contents from being erased by doing the following:

[self setClearsContextBeforeDrawing: NO];

This is merely a hint to the graphics engine that there is no point in having it pre-clear the view for you, since you will likely need to re-draw the whole area anyway. It may prevent your view from being automatically erased, but you cannot depend on it.

Community
  • 1
  • 1
mspasov
  • 451
  • 6
  • 19
  • good... this is an important concept... I think back in the old days when I was doing Win32 programming, I could draw directly on the screen... let me check if this is possible on iOS – Jeremy L Apr 22 '12 at 08:46