This chapter discusses drawing issues when using Core Animation and other technologies.

Drawing Layer Content With Application Kit Classes

Core Animation CALayer class defines a delegate method, drawLayer:inContext:, that you can implement and draw your layer content using Quartz 2D drawing functions. However, Cocoa developers who have complete and working drawing solutions based on the Application Kit drawing classes may wish to continue using that code.

Listing 1 shows an implementation of the CALayer delegate method drawLayer:inContext: that creates an NSGraphicsContext from the CGContextRef passed as the inContext: parameter. Layer delegates can use this technique to display content created using NSBezierPath, NSColor, NSImage and other Application Kit classes.

Listing 1  Drawing into a layer using Application Kit classes

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
   NSGraphicsContext *nsGraphicsContext;
   nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:ctx
   [NSGraphicsContext saveGraphicsState];
   [NSGraphicsContext setCurrentContext:nsGraphicsContext];
   // ...Draw content using NS APIs...
   NSRect aRect=NSMakeRect(10.0,10.0,30.0,30.0);
   NSBezierPath *thePath=[NSBezierPath bezierPathWithRect:aRect];
   [[NSColor redColor] set];
   [thePath fill];
   [NSGraphicsContext restoreGraphicsState];