• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Drawing in a NSView out side of drawRect
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drawing in a NSView out side of drawRect


  • Subject: Re: Drawing in a NSView out side of drawRect
  • From: Ricky Sharp <email@hidden>
  • Date: Fri, 19 Dec 2008 18:36:43 -0600


On Dec 19, 2008, at 6:20 PM, David Alter wrote:

I want to draw in a NSView but not when drawRect is called. To do this I
understand that I need to call lockFocus before drawing and unlockFocus
after. The drawing appears to happen but it is not until I deactivate the
window do I see my results. How can I get it to refresh once I have done my
drawing?
To test this out I have sub classed NSView and overloaded mouseDown. I added
the following code.


- (void)mouseDown:(NSEvent *)theEvent ;

{

   NSPoint loc = [[self window] mouseLocationOutsideOfEventStream];

   loc = [self convertPoint:loc fromView:[[self window] contentView]];

    CGContextRef myContext =
(CGContextRef)[[NSGraphicsContext currentContext]graphicsPort];

   [self lockFocus];

   CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);

   CGContextFillRect (myContext, CGRectMake (loc.x, loc.y, 10, 10 ));

   [self unlockFocus];

}

This will draw a box for each mouse click, but not until I deactivate my
window.


What you should do is do all drawing in drawRect:. What happens when your drawRect: is called? I bet you won't see any of the rects you had when clicking around.

Instead, maintain a list of rects that need to be drawn. mouseDown: then simply adds a new rect to the list and calls setNeedsDisplay:. Or, if you profile things and need more speed, setNeedsDisplayInRect:

If you want things such that only one rect ever is drawn, just have mouseDown: set the values of that single rect and still call setNeedsDisplay:

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Drawing in a NSView out side of drawRect (From: "David Alter" <email@hidden>)

  • Prev by Date: Using NSURL authentication APIs for non-standard protocols
  • Next by Date: Using runtime functions to match up object types safely when setting properties
  • Previous by thread: Drawing in a NSView out side of drawRect
  • Next by thread: Re: Drawing in a NSView out side of drawRect
  • Index(es):
    • Date
    • Thread