Custom view, drawRect, clip?

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

    Everything I need to know is indeed in that document.
    Thanks and sorry for not having read the doc more carefully.

    -Martin.
  • > Everything I need to know is indeed in that document.
    > Thanks and sorry for not having read the doc more carefully.

      For what it's worth (and not to sound like a smart-a**), the page
    was found immediately by googling the phrase "cooca drawing clip" - it
    was the top result (above some links for hot cocoa and cookie clip
    art, apparently). I already knew the drawing guide would have the
    answer, though I haven't read it in awhile, but I didn't know where.
    Google knows, though.

      Then again, Google just sent me on a merry chase for a phantom
    restaurant that turned out to be a parking garage during lunch today
    ...

    --
    I.S.
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !