• 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: Stopping a NSView from drawing in -drawRect
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stopping a NSView from drawing in -drawRect


  • Subject: Re: Stopping a NSView from drawing in -drawRect
  • From: Troy Stephens <email@hidden>
  • Date: Fri, 6 Feb 2004 14:40:26 -0800

On Feb 6, 2004, at 2:30 PM, Phillip Hutchings wrote:
A -drawRect: method should not call -lockFocus / -unlockFocus. AppKit automatically locks focus before calling your view's -drawRect: method, and unlocks it on return.

Oops, a slight leftover from earlier when I was doing something very weird there :P

:-)

Sending -display from inside a -drawRect: method is also not a practice I'd recommend. What is trackTextView's relationship to the present view? If you make it a subview, AppKit will draw it automatically. If you need to composite an image on top of the TextView's content, you could add an ImageView as a subview of the TextView, or subclass NSTextView to draw the image.

It is a subview, and I've now removed it. I think it was there because I was doing something odd and flushing the display when I shouldn't somehow. Anyway, it works fine without it.

The "-display..." methods are occasionally useful for forcing immediate drawing, but most of the time it's better to defer drawing via the -setNeedsDisplay[InRect]: methods.

If you do all your drawing in response to receiving -drawRect: messages, request drawing via -setNeedsDisplay: and/or -setNeedsDisplayInRect: instead of -display, and eliminate the redundant -lockFocus/-unlockFocus, this should I think eliminate the flickering, as AppKit will flush once at the end of each window drawing pass. Let me know if that doesn't fix the problem for some reason; maybe I don't fully understand what you're trying to do.

It has worked fine, my drawing is now:
- (void)drawRect:(NSRect)rect
{
[bgColour set];
[NSBezierPath fillRect:rect];
[fgColour set];
if (gotImage == YES) {
[theArtwork compositeToPoint:imagePoint operation:NSCompositeCopy];
}
}

That looks much better.

I think my problem was calling [self drawRect: [self bounds]] instead of [self setNeedsDisplay:YES] in -animateOneFrame (this is a screeensaver) for some reason.

-drawRect: assumes and requires some setup that AppKit provides before calling it. It isn't intended to be called directly by an app's own code. If you need to request drawing of a view, either send it a -setNeedsDisplay[InRect]: message, or (if necessary) a -display... message.

Well, I'll learn ;)

:-)
The "Drawing and Views" topic might provide a helpful refresher:
http://developer.apple.com/documentation/Cocoa/Conceptual/DrawViews/ index.html

Glad to hear your app is working well now!

Troy Stephens
Cocoa frameworks, Apple
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Stopping a NSView from drawing in -drawRect (From: Phillip Hutchings <email@hidden>)
 >Re: Stopping a NSView from drawing in -drawRect (From: James Chen <email@hidden>)
 >Re: Stopping a NSView from drawing in -drawRect (From: Phillip Hutchings <email@hidden>)
 >Re: Stopping a NSView from drawing in -drawRect (From: Troy Stephens <email@hidden>)
 >Re: Stopping a NSView from drawing in -drawRect (From: Phillip Hutchings <email@hidden>)

  • Prev by Date: Re: Stopping a NSView from drawing in -drawRect
  • Next by Date: Re: malloc in obj-c/cocoa
  • Previous by thread: Re: Stopping a NSView from drawing in -drawRect
  • Next by thread: what method is called when window is re-loaded?
  • Index(es):
    • Date
    • Thread