Working with multiple drawRect calls

I found that my drawRect is called more than once. Unfortunately, this had an unfortunate side effect of double-drawing everything, because all my subtasks are drawn in drawRect (I'm a strict atheist wrt Interface Builder).

What is the best way to handle multiple casting calls? Flag to check if it will be called again? Or clear the whole view and redraw from scratch (how did I do this?)

+4
source share
2 answers

What do you mean by the word "your subtitles are drawn in drawRect"? If you mean that you are invoking calls to -addSubview: in your drawRect, do not. Move them to a more suitable place, which is called only when necessary (perhaps the -initWithFrame: method for your view, if there is always a subview) and use drawRect only to create a custom drawing.

Strictly speaking, it is called several times - this is the whole point -drawRect. He encouraged updating small parts of your presentation when necessary. It is often called quite often if you update your view (moving it, updating the supervisor, etc.), therefore it should be as simple and fast as possible to avoid performance problems.

+2
source

When UIKit calls drawRect: graphics context configured for drawing should already be cleared for you (unless you set the clearsContextBeforeDrawing property to NO ). Perhaps you call drawRect: manually instead of calling setNeedsDisplay / setNeedsDisplayInRect: :?

In addition, each view is responsible for drawing only its own content, and not the contents of its subzones.

0
source

Source: https://habr.com/ru/post/1316723/


All Articles