Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Drawing
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 |
flipped:NO]; |
[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]; |
} |
Copyright © 2015 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2015-03-09