Skip navigation.
 
mlCustom view, drawRect, clip?
FROM : Martin
DATE : Mon Feb 04 19:45:16 2008

Hi,

I have a custom view in which I do my drawing in the -(void)drawRect:
(NSRect)rect method. For a start, I just want to draw a huge gradient 
in that view. So I just do:
- (void)drawRect:(NSRect)rect {
   [fancyGradient drawInRect:rect angle:270.0];
}

But then, Cocoa needs to refresh some small parts of that view, so it 
calls drawRect: with rect being much smaller than the view's frame. If 
I keep the previous version of drawRect, the result produced is not 
what I want: since fancyGradient is computed for rect, with rect being 
smaller that my view's frame, a small gradient is overlayed on top of 
the big one.

I have one solution for that problem :
- (void)drawRect:(NSRect)rect {
   rect.origin.y = 0;
   rect.size.height = [self frame].size.height;
   [fancyGradient drawInRect:rect angle:270.0];
}

My first question is: is it bad to draw outside the initial rect?

My second question is: should I create the gradient first, then clip 
it, then draw it? Can I clip NSGradient? (If no, do I have to use 
CGGradient and CoreGraphics instead ?)

My third question is: I have the same problem with a NSBezierPath. How 
do I clip it? What does [myPath addClip] do (yes, I've read the doc, 
so my question is: is clipping path of the current graphic context 
automagically set to rect)? What is the difference with [NSBezierPath 
clipRect:rect]?

Thanks,
Martin.

Related mailsAuthorDate
mlCustom view, drawRect, clip? Martin Feb 4, 19:45
mlRe: Custom view, drawRect, clip? I. Savant Feb 4, 19:54
mlRe: Custom view, drawRect, clip? Martin Feb 4, 20:02
mlRe: Custom view, drawRect, clip? I. Savant Feb 4, 20:09