To get around layout problems, as well as the fact that using -renderInContext:layers to draw hierarchies does not preserve vector elements, we subclassed CALayer into Basic Layout Basics . The CPLayer subclass overrides the default method -drawInContext:to invoke our own method -renderAsVectorInContext:(where we make all our Core Graphics drawings for the layer). To create a PDF context (or similar) for printing, we call a custom method with the following code:
-(void)recursivelyRenderInContext:(CGContextRef)context
{
CGContextSaveGState(context);
[self applyTransform:self.transform toContext:context];
self.renderingRecursively = YES;
if ( !self.masksToBounds ) {
CGContextSaveGState(context);
}
[self renderAsVectorInContext:context];
if ( !self.masksToBounds ) {
CGContextRestoreGState(context);
}
self.renderingRecursively = NO;
for ( CALayer *currentSublayer in self.sublayers ) {
CGContextSaveGState(context);
CGPoint currentSublayerFrameOrigin = currentSublayer.frame.origin;
CGRect currentSublayerBounds = currentSublayer.bounds;
CGContextTranslateCTM(context,
currentSublayerFrameOrigin.x - currentSublayerBounds.origin.x,
currentSublayerFrameOrigin.y - currentSublayerBounds.origin.y);
[self applyTransform:self.sublayerTransform toContext:context];
if ( [currentSublayer isKindOfClass:[CPLayer class]] ) {
[(CPLayer *)currentSublayer recursivelyRenderInContext:context];
} else {
if ( self.masksToBounds ) {
CGContextClipToRect(context, currentSublayer.bounds);
}
[currentSublayer drawInContext:context];
}
CGContextRestoreGState(context);
}
CGContextRestoreGState(context);
}
Core Graphics, , .
, , , , . , , position, , . , , , .