push到一个页面进行绘图时,设置如下:

CALayer * layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 300, 300);
layer.delegate = self;
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
-(void) drawLayer: (CALayer*) layer inContext: (CGContextRef) context;  

设置delegate后,点击返回按钮时会奔溃。没有push直接绘制的时候就没有问题。

设置其delegate为uiview类型实例。会导致程序crash。

关于这一点,苹果官方在文档中已有说明。解决办法:

he lightest-wight solution would be to create a small helper class in the the file as the UIView that's using the CALayer:

In MyView.h

@interface MyLayerDelegate : NSObject
. . .
@end
In MyView.m

@implementation MyLayerDelegate
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
{
. . .
}
@end
Just place those at the top of your file, immediately below the #import directives. That way it feels more like using a "private class" to handle the drawing (although it isn't -- the delegate class can be instantiated by any code that imports the header).

没怎么看懂:

直接继承CALayer或者CAShapeLayer重写:

- (void)drawInContext:(CGContextRef)ctx

就可以