在使用层来手动绘制视图时,如果有添加子层,并且希望使用使用
-(void) drawLayer: (CALayer*) layer inContext: (CGContextRef) context
来自己用代码绘制视图时,注意不要设置其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).