I'm trying to fill an NSView with a gradient. when the window is in the background the gradient should have lighter colors to match the rest of the window. The code below has a lot of artifacts: When the window is first drawn it is drawn with the background colors. When the window is resized, the foreground colors are used. When the window is moved to the back, the background colors are not used as expected. Should I not be using isKeyWindow for this task?
- (void)drawRect:(NSRect)dirtyRect {
if ([[self window] isKeyWindow]) {
NSColor *startingColor = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0];
NSColor *endingColor = [NSColor colorWithCalibratedWhite:0.6 alpha:1.0];
NSGradient* aGradient = [[NSGradient alloc]
initWithStartingColor:startingColor
endingColor:endingColor];
[aGradient drawInRect:[self bounds] angle:270];
} else {
NSColor *startingColor = [NSColor colorWithCalibratedWhite:0.9 alpha:1.0];
NSColor *endingColor = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0];
NSGradient* aGradient = [[NSGradient alloc]
initWithStartingColor:startingColor
endingColor:endingColor];
[aGradient drawInRect:[self bounds] angle:270];
}
[super drawRect:dirtyRect];
}