iOS自定义CALayer类崩溃
我正在尝试使用以下技术创建自定义绘制的图标: 然而,StarLayer示例正在崩溃(消息被发送到deallocated实例),我无法找出问题所在。StarLayer以某种方式被解除分配。我用的是ARC StarLayer是NSObject的子类,它实现了以下方法:iOS自定义CALayer类崩溃,ios,crash,calayer,Ios,Crash,Calayer,我正在尝试使用以下技术创建自定义绘制的图标: 然而,StarLayer示例正在崩溃(消息被发送到deallocated实例),我无法找出问题所在。StarLayer以某种方式被解除分配。我用的是ARC StarLayer是NSObject的子类,它实现了以下方法: - (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)context 使用CALayers创建自定义图标的方法正确吗 编辑: 更多详情: 以下是
- (void)drawLayer:(CALayer *)theLayer
inContext:(CGContextRef)context
使用CALayers创建自定义图标的方法正确吗
编辑:
更多详情:
以下是使用星形图层的视图控制器:
#import "ViewController.h"
#import "StarLayer.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
StarLayer *star = [[StarLayer alloc] initWithRect:self.view.frame];
[self.view.layer addSublayer:star.root];
}
@end
下面是星形层实现:
#import "StarLayer.h"
#import <QuartzCore/QuartzCore.h>
@implementation StarLayer
@synthesize root = _rootLayer;
-(id)initWithRect:(CGRect)rect {
self = [super init];
if (self) {
// Initialization code
[self setupLayers:rect];
}
return self;
}
-(void)setupLayers:(CGRect)rect {
_rootLayer = [CALayer layer];
_rootLayer.frame = rect;
_starLayer = [CALayer layer];
_starLayer.frame = CGRectMake(0,0,rect.size.width,rect.size.height);
_starLayer.delegate = self;
[_starLayer setNeedsDisplay];
float TWOPI = 2 * 3.1415926535;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration=8.0;
animation.repeatCount=HUGE_VALF;
animation.autoreverses=NO;
animation.fromValue=[NSNumber numberWithFloat:0.0];
animation.toValue=[NSNumber numberWithFloat:TWOPI];
[_starLayer addAnimation:animation forKey:@"rotation"];
[_rootLayer addSublayer:_starLayer];
_textLayer = [CALayer layer];
_textLayer.delegate = self;
_textLayer.frame = CGRectMake(0, 0, rect.size.width,rect.size.height);
[_textLayer setNeedsDisplay];
[_rootLayer addSublayer:_textLayer];
}
- (void)drawLayer:(CALayer *)theLayer
inContext:(CGContextRef)context
{
if (theLayer == _textLayer)
{
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
UI Font *font = [UIFont fontWithName:@"Helvetica" size:24.0];
NSString *s = @"Hello!";
CGSize sz = [s sizeWithFont:font
constrainedToSize:theLayer.bounds.size
lineBreakMode:UILineBreakModeClip];
CGRect r = CGRectOffset(CGRectMake(0, 0, theLayer.bounds.size.width, sz.height), 0, (theLayer.bounds.size.height-sz.height)/2);
[s drawInRect:r
withFont:font
lineBreakMode:UILineBreakModeClip
alignment:UITextAlignmentCenter];
CGContextRestoreGState(context);
UIGraphicsPopContext();
return;
}
CGRect frame = theLayer.frame;
CGPoint offset = CGPointMake(frame.size.width/2, frame.size.height/2);
int r1 = frame.size.width/2;
int r2 = r1 - 20;
int npoints = 60;
float TWOPI = 2 * 3.1415926535;
CGMutablePathRef r = CGPathCreateMutable();
for (float n=0; n < npoints; n+=3)
{
int x1 = offset.x + sin((TWOPI/npoints) * n) * r2;
int y1 = offset.y + cos((TWOPI/npoints) * n) * r2;
if (n==0)
CGPathMoveToPoint(r, NULL, x1, y1);
else
CGPathAddLineToPoint(r, NULL, x1, y1);
int x2 = offset.x + sin((TWOPI/npoints) * n+1) * r1;
int y2 = offset.y + cos((TWOPI/npoints) * n+1) * r1;
CGPathAddLineToPoint(r, NULL, x2, y2);
int x3 = offset.x + sin((TWOPI/npoints) * n+2) * r2;
int y3 = offset.y + cos((TWOPI/npoints) * n+2) * r2;
CGPathAddLineToPoint(r, NULL, x3, y3);
}
CGPathCloseSubpath(r);
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(4, 4), 5);
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextAddPath(context, r);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
@end
#导入“StarLayer.h”
#进口
@实现StarLayer
@合成根=根层;
-(id)initWithRect:(CGRect)rect{
self=[super init];
如果(自我){
//初始化代码
[自设置层:rect];
}
回归自我;
}
-(void)setupLayers:(CGRect)rect{
_根层=[CALayer层];
_rootLayer.frame=rect;
_星层=[CALayer层];
_starLayer.frame=CGRectMake(0,0,rect.size.width,rect.size.height);
_starLayer.delegate=self;
[_StarlayerSetNeedsDisplay];
浮点数TWOPI=2*3.1415926535;
CABasicAnimation*动画=[CABasicAnimation animationWithKeyPath:@“transform.rotation”];
动画。持续时间=8.0;
animation.repeatCount=巨大的值;
animation.autoreverses=否;
animation.fromValue=[NSNumber numberWithFloat:0.0];
animation.toValue=[NSNumber numberWithFloat:TWOPI];
[_starlayeraddanimation:animation forKey:@“旋转”];
[_rootlayeraddsublayer:_starLayer];
_textLayer=[CALayer层];
_textLayer.delegate=self;
_textLayer.frame=CGRectMake(0,0,rect.size.width,rect.size.height);
[_textlayersetneedsdisplay];
[_rootlayeraddsublayer:_textLayer];
}
-(空)绘图层:(CALayer*)该层
inContext:(CGContextRef)上下文
{
如果(图层==\u文本图层)
{
UIGraphicsPushContext(context);
CGContextSaveGState(上下文);
CGContextSetFillColorWithColor(上下文,[[UIColor blackColor]CGColor]);
UI Font*Font=[UIFont fontWithName:@“Helvetica”大小:24.0];
NSString*s=@“你好!”;
CGSize sz=[s sizeWithFont:font
constrainedToSize:theLayer.bounds.size
lineBreakMode:UILineBreakModeClip];
CGRect r=CGRectOffset(CGRectMake(0,0,theLayer.bounds.size.width,sz.height),0,(theLayer.bounds.size.height sz.height)/2);
[s图纸内容:r]
withFont:font
lineBreakMode:UILineBreakModeClip
对齐方式:UITextAlignmentCenter];
CGContextRestoreGState(上下文);
UIGraphicsPopContext();
返回;
}
CGRect frame=layer.frame;
CGPoint offset=CGPointMake(frame.size.width/2,frame.size.height/2);
int r1=frame.size.width/2;
int r2=r1-20;
int npoints=60;
浮点数TWOPI=2*3.1415926535;
CGMutablePathRef r=CGPathCreateMutable();
对于(浮点n=0;n
和僵尸:
你很难期望人们去阅读教程,下载项目,然后自己找到崩溃。您必须包含更多关于崩溃和代码相关部分的信息。你有没有使用僵尸来查找谁给解除分配的对象发送消息。当我添加星型子层时,它在ViewDidLoad崩溃。它被解除分配,不知道为什么。我已编辑了我的问题并添加了一些详细信息。在我看来,似乎没有任何内容保留对
star
的引用。您正在将star.root
作为子层添加到self.view.layer
,但是没有任何东西可以保持star
。尝试将star
放入ivar或属性。就是这样!我已经将它添加到ivar中,并且它可以正常工作。我认为在子层中添加星星层会增加保留数。将你的评论复制到答案中,这样我就可以将其标记为正确答案。