Legacy Document
Important: This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.
Current information on this Reference Library topic can be found here:
|
IMPORTANT: BackgroundMac OS X 10.3 introduced a more optimized drawing model for Cocoa views. Supported by new The The ProblemAs an efficiency measure, Overriding -needsToDrawRect: to Perform CorrectlyYou can work around this problem by overriding If a view is rarely asked to draw more than a few rectangles at a time, and its content is not particularly complex or expensive to draw, the following reimplementation of Listing 1: A Simple - (BOOL)needsToDrawRect:(NSRect)rect
{
const NSRect *rectList;
int count;
int i;
[self getRectsBeingDrawn:&rectList count:&count];
for (i = 0; i < count; i++) {
if (NSIntersectsRect(rect, rectList[i])) {
return YES;
}
}
return NO;
}
The above approach has the advantage of not requiring any changes to code that calls To improve performance in cases where a view is typically given many rectangles to draw at once, you may choose to define and use an alternative to this method that quickly rejects objects that lie outside the rectList's bounding rect. You would invoke this method instead of Listing 2: A More Optimized - (BOOL)needsToDrawRect:(NSRect)rect rectListBounds:(NSRect)rectListBounds
{
const NSRect *rectList;
int count;
int i;
if (!NSIntersectsRect(rect, rectListBounds)) {
return NO;
}
[self getRectsBeingDrawn:&rectList count:&count];
if (count == 1) {
return YES;
} else {
for (i = 0; i < count; i++) {
if (NSIntersectsRect(rect, rectList[i])) {
return YES;
}
}
return NO;
}
}
Document Revision History
| ||||||||||||