Drawing outside of drawRect for NSView..

  • Is it possible to draw to a view outside of drawRect?  For instance,
    what if I wanted to draw an NSRect when the mouseDown event is
    fired?  Is this kind of thing bad practice?  I've been trying to draw
    an NSRect to my view during the mouseDown event, but for reasons
    unknown to me the NSRect is being applied to the window and its
    coordinate system rather than my view's (and yes I used
    convertPoint:toView:).  Thanks in advance.

    ----------------------------------
    Carter R. Harrison
    <carterharrison...>
    ----------------------------------
  • Carter:

      This is not the correct approach. Check out the Sketch example
    code in your Developer folder. Take your time to ferret out the
    selection code (for drag-selecting shapes in the drawing).

      Basically, you want to define the starting point during your
    mouseDown: event. You'll also want a BOOL like "isSelecting". As the
    user continues dragging, keep updating the end point and flag your
    view as needing displayed. When the mouse button is released (ie
    NSLeftMouseUp), set isSelecting to NO and flag for display again.

      In your drawRect: method, if "isSelecting", you take those two
    points, determine your rectangle, and draw your necessary selection
    adornments. If it's not selecting, don't draw the adornments -- since
    you'll be marking your view as needing display in the right places in
    your mouseDown: loop, the selection rectangle will 'disappear' when
    the mouse is released.

      I hope this helps.

    --
    I.S.

    On Oct 15, 2006, at 9:35 AM, Carter R. Harrison wrote:

    > Is it possible to draw to a view outside of drawRect?  For
    > instance, what if I wanted to draw an NSRect when the mouseDown
    > event is fired?  Is this kind of thing bad practice?  I've been
    > trying to draw an NSRect to my view during the mouseDown event, but
    > for reasons unknown to me the NSRect is being applied to the window
    > and its coordinate system rather than my view's (and yes I used
    > convertPoint:toView:).  Thanks in advance.
    >
    > ----------------------------------
    > Carter R. Harrison
    > <carterharrison...>
    > ----------------------------------
    >
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/idiotsavant2005%
    > 40gmail.com
    >
    > This email sent to <idiotsavant2005...>
previous month october 2006 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
30 31          
Go to today