On Tue, Dec 23, 2008 at 12:44 PM, David Alter <alterconsult...@gmail.com> 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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com