關於CALayer致使的crash問題

push到一個頁面進行繪圖時,設置以下:iview

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直接繪製的時候就沒有問題。ui

設置其delegate爲uiview類型實例。會致使程序crash。spa

關於這一點,蘋果官方在文檔中已有說明。解決辦法:code

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).

沒怎麼看懂:blog

直接繼承CALayer或者CAShapeLayer重寫:繼承

- (void)drawInContext:(CGContextRef)ctx

就能夠文檔

相關文章
相關標籤/搜索