How do I stop setNeedsDisplayInRect / drawRect coalescence?
When programming my app, I get to the point where I have to update two rectangles on the screen. So I call [self setNeedsDisplayInRect:rect1] and then [self setNeedsDisplayInRect:rect2]. When my drawRect method is called, the rectangle parameter is the smallest rectangle which contains both rect1 and rect2.
I can handle this with no problem, but when the two rectangles are far apart, then I am updating a lot开发者_运维百科 of real estate with no gain. In this case, I would just like to repaint my two small rectangles.
So my question is how can I prevent the underlying system from coalescing my two calls into one?
You don't have to prevent the system from coalescing the calls because in -drawRect:
, you can query the individual regions that have to be updated by calling -getRectsBeingDrawn:count:
. This will return your individual rectangles rect1
and rect2
.
Note that -getRectsBeingDrawn:count:
is guaranteed to return non-overlapping rectangles. This seems to imply that if, for example, you called -setNeedsDisplayInRect:
twice for the same rectangle, -getRectsBeingDrawn:count:
would only return that rectangle once. In other words, you don't need to worry that you're drawing the same region twice.
精彩评论