MouseMoved NSView subclass

Discussion in 'Mac Programming' started by MrFusion, Sep 19, 2007.

  1. MrFusion macrumors 6502a

    Jun 8, 2005
    I have a subclass of NSView that is able to track the mouse.
    In the mouseMoved method I can determine the mouse position from the NSEvent.

    Now I want to draw something at that position, however the coordinate system seems to have shifted. In a normal NSView subclass, you can only draw within the view itself and the origin of the coordinate system corresponds to the lower left corner of the NSView. When I do any drawing in the mouseMoved, it's relative to the lower left corner of the window.

    Any trick/idea as to how I can find out where my NSView is, or to restrict drawing in the manner accustomed to the NSView subclass?


    Another case of formulating the question to find the answer, although it's an inefficient one.

    Adding a variable to class that holds a NSPoint currentLocation.
    In mouseMoved: set the currentLocation and set needsDisplay: to yes for the view.

    But this causes the entire view to redraw, which is unnecessary. Only a small portion needs to redraw itself.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Are you using [self convertPoint:[event locationInWindow] fromView:nil] in your mouseMoved event?

    OK about the drawing part... drawRect: gets called when some part of the view needs to be redrawn. Quartz is smart enough to pass you the rect of what part needs to be drawn. However, the majority of the time you will always redraw everything. To avoid this, create some smart code that only redraws certain parts based on the passed in rect, or use subviews (easier).
  3. MrFusion thread starter macrumors 6502a

    Jun 8, 2005
    I found out yesterday while working on the problem in this thread.
    .I now call [frame bounds] at the beginning of my drawRect:
    In the next iteration I'll change this so that only the necessary part is redrawn.
    The more features I add, the more I learn about drawing. The greater complexity/feature range casuses me to rework the class anyway.

Share This Page