CABasicAnimation动画中的上下文错误

时间:2013-05-22 19:07:51

标签: ios calayer cgcontext cabasicanimation

我有中风动画:

- (void)drawBezierAnimate:(BOOL)animate
{

UIColor* color = [UIColor colorWithRed: 1 green: 0.562 blue: 0.343 alpha: 1];

UIBezierPath *bezierPath = [self bezierPath];


CAShapeLayer *bezier = [[CAShapeLayer alloc] init];

bezier.path          = bezierPath.CGPath;
bezier.lineCap       = kCALineCapRound;
bezier.strokeColor   = color.CGColor;
bezier.fillColor     = [UIColor clearColor].CGColor;
bezier.strokeStart   = 1.0;
bezier.strokeEnd     = 0.0;
bezier.lineWidth     = 8.0;
bezier.strokeStart   = 0.0;
bezier.strokeEnd     = 1.0;
[self.view.layer addSublayer:bezier];


if (animate)
   {
    CABasicAnimation *animateStrokeStart = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    animateStrokeStart.duration  = appDelegate.myPlayer.audioPlayer.duration;
    animateStrokeStart.fromValue = [NSNumber numberWithFloat:1.0f];
    animateStrokeStart.toValue   = [NSNumber numberWithFloat:0.0f];
    [bezier addAnimation:animateStrokeStart forKey:@"strokeStartAnimation"];
   }
}

动画效果很好,但我收到了错误:

<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetLineWidth: invalid context 0x0
<Error>: CGContextSetLineJoin: invalid context 0x0
<Error>: CGContextSetLineCap: invalid context 0x0
<Error>: CGContextSetMiterLimit: invalid context 0x0
<Error>: CGContextSetFlatness: invalid context 0x0
<Error>: CGContextAddPath: invalid context 0x0
<Error>: CGContextDrawPath: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

动画的路径是:

   - (UIBezierPath *)bezierPath
{


    //// Color Declarations
    UIColor* color0 = [UIColor colorWithRed: 0.756 green: 0.756 blue: 0.756 alpha: 0.6];



    //// Bezier Drawing
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];


    [bezierPath moveToPoint: CGPointMake(213.79, 170.83)];
    [bezierPath addCurveToPoint: CGPointMake(131.93, 95) controlPoint1: CGPointMake(212.14, 128.68) controlPoint2: CGPointMake(176.13, 95)];
    [bezierPath addCurveToPoint: CGPointMake(50, 173.85) controlPoint1: CGPointMake(86.68, 95) controlPoint2: CGPointMake(50, 130.3)];
    [bezierPath addCurveToPoint: CGPointMake(131.93, 252.7) controlPoint1: CGPointMake(50, 217.4) controlPoint2: CGPointMake(86.68, 252.7)];
    [bezierPath addCurveToPoint: CGPointMake(157.55, 248.76) controlPoint1: CGPointMake(140.88, 252.7) controlPoint2: CGPointMake(149.49, 251.32)];
    [bezierPath addCurveToPoint: CGPointMake(209.69, 281) controlPoint1: CGPointMake(166.59, 267.78) controlPoint2: CGPointMake(186.54, 281)];
    [bezierPath addCurveToPoint: CGPointMake(267, 225.85) controlPoint1: CGPointMake(241.34, 281) controlPoint2: CGPointMake(267, 256.31)];
    [bezierPath addCurveToPoint: CGPointMake(213.79, 170.83) controlPoint1: CGPointMake(267, 196.71) controlPoint2: CGPointMake(243.53, 172.86)];
    [bezierPath closePath];


    [color0 setStroke];
    bezierPath.lineWidth = 11;
    [bezierPath stroke];

    return bezierPath;
}

我知道如果我想画一些东西,我需要获得currentContext,但这是一个动画,我甚至没有drawRect:

如何摆脱这些恼人的错误?

1 个答案:

答案 0 :(得分:1)

问题取决于bezierPath方法。删除说:

的行
[color0 setStroke];
bezierPath.lineWidth = 11;
[bezierPath stroke];

CAShapeLayer为您完成所有绘图。 bezierPath应该只定义路径本身,但不执行任何可视化渲染。让CAShapeLayer为你做这件事。