iOS自定义CALayer类CRASH

时间:2013-09-27 10:30:34

标签: ios crash calayer

我正在尝试使用此技术创建自定义绘制的图标: Drawing animated shapes and text in Core Animation layers

然而,StarLayer示例崩溃(消息发送到解除分配的实例),我无法找出问题所在。 StarLayer以某种方式被取消分配。我正在使用ARC。

StarLayer是NSObject的子类,它实现了方法:

- (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

僵尸: enter image description here

0 个答案:

没有答案