Boon
Boon

Reputation: 41480

How to prevent UIView's drawRect from clearing out the paths drawn by previous drawRect calls?

Everytime UIView's drawRect is called, the content drawn by previous drawRect (using Core Graphics) is cleared out. How can I make it so that the paths rendered from previous drawRect calls stay until I explicitly clear it?

Upvotes: 6

Views: 3926

Answers (3)

Lounges
Lounges

Reputation: 4664

I'm fighting this issue myself right now. The problem is there is a property on the UIView called "clearsContextBeforeDrawing" that according to the documentation is supposed to fix this problem, however it doesn't work that way in my experience.

I think ultimately the solution to this is going to be to allocate an offscreen buffer and do all my drawing there, then blt it over to the UIView in the drawRect method.

Upvotes: 1

Mike Abdullah
Mike Abdullah

Reputation: 15003

I'm pretty certain there's no way around this; it's how UIView is designed to work. If asked to, your custom view should be able to draw any part of itself at any time. This is partly because views can do more than just appear onscreen. e.g. on the desktop they can be printed, and even on the iPhone, you might wish to capture the contents of a view to a bitmap.

Upvotes: 0

alamodey
alamodey

Reputation: 14938

You basically need to clip the 'dirty' part of your rect where changes have been made, and only this part will be re-drawn.

- (void)setNeedsDisplayInRect:(CGRect)invalidRect

Upvotes: 3

Related Questions