Skip navigation.
 
mlRe: Custom view, drawRect, clip?
FROM : I. Savant
DATE : Mon Feb 04 19:54:56 2008

> - (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.


  The Cocoa Drawing Guide explains the reasons for this in great
detail. The short version: Cocoa only asks you to draw what's "dirty".
It's up to you to give your view intelligence enough to deal with
drawing all or part of itself.

> 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?


  Not at all, but there's an easier way:

- (void)drawRect:(NSRect)rect
{
  [fancyGradient drawInRect:[self bounds] angle:270.0];
}

> 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]?


  Again, read the Cocoa Drawing Guide:

http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide/GraphicsContexts/chapter_3_section_3.html#

  The above page covers this in detail.

--
I.S.

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