Re: Drawing in a NSView out side of drawRect
Re: Drawing in a NSView out side of drawRect
- Subject: Re: Drawing in a NSView out side of drawRect
- From: "Michael Ash" <email@hidden>
- Date: Wed, 24 Dec 2008 00:30:30 -0500
On Tue, Dec 23, 2008 at 12:44 PM, David Alter <email@hidden> wrote:
> Thanks to everyone that has responded to me. It has been a big help.
>
>> > I want to draw in a NSView but not when drawRect is called.
>>
>> Others have pointed this out, but I want to reiterate: no you don't.
>> Trying to draw outside of drawRect: will only lead to pain. (It is
>> useful, on rare occasions. You are not experiencing one of them.) Work
>> with the view machinery. Call -setNeedsDisplay:.
>
> I understand that the preferred method of doing thing is do your drawing in
> drawRect:. I would like to understand better what are the issues and draw
> backs to using lockFocus and then drawing.
The major problem is that the system can ask your view to redisplay at
any time. For example, your view might get redisplayed because the
user resized your window, or deminimized it, or even just moved a
window that's sitting on top of it (although in OS X, that last one
doesn't actually apply).
So your view needs to be prepared to completely recreate its content
at any time using the drawRect: method.
Given that requirement, using -lockFocus *usually* means something is
wrong. If the code that you have inside the lock/unlockFocus pair is
not duplicated in -drawRect:, then you won't be able to redraw your
contents on demand, and your view could end up looking very strange to
the user. If it *is* duplicated in -drawRect:, well, code duplication
is a bad thing, and anyway if the necessary stuff is in -drawRect:
then you can simply mark the view as needing to be redisplayed instead
of drawing directly.
There's also an efficiency advantage to not using lockFocus. Multiple
invalidations to the same view within the same event processing cycle
will result in a single call to -drawRect: and a single flush of the
window to the screen. This coalescing can't be done when calling
lockFocus.
Mike
_______________________________________________
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