如何在CABasicAnimation启动之前阻止CAShapeLayer绘制

时间:2014-04-08 09:12:16

标签: ios objective-c cabasicanimation cashapelayer

我写了#34;空间"分离的进度循环,其中我按顺序对进度进行动画处理。

方法被调用3次,具有不同的进度和弧数。 问题是 - BezierPaths在动画开始之前绘制了自己的东西,当它开始给出不好的效果时它们会消失。

有没有办法阻止它们重新绘制,直到动画开始?

- (void)drawArcNo:(NSInteger)number withProgress:(CGFloat)progress
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];

    CGFloat angle = (number * 120) - 85;

    [bezierPath addArcWithCenter:CGPointMake(self.circleBackgroundView.bounds.size.width / 2, self.circleBackgroundView.bounds.size.height / 2) radius:50.0 startAngle:degreesToRadians(angle) endAngle:degreesToRadians((angle + 115) * progress) clockwise:YES];

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

    [progressLayer setPath:bezierPath.CGPath];

    [progressLayer setStrokeColor:[UIColor redColor].CGColor];
    [progressLayer setFillColor:[UIColor clearColor].CGColor];
    [progressLayer setLineWidth:5];

    [progressLayer setStrokeStart:0.0];
    [progressLayer setStrokeEnd:1.0];

    [self.circleBackgroundView.layer addSublayer:progressLayer];

    CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animateStrokeEnd.fillMode = kCAFillModeForwards;
    animateStrokeEnd.removedOnCompletion = NO;
    animateStrokeEnd.beginTime = CACurrentMediaTime() + number;
    animateStrokeEnd.duration  = 1 * progress;  //kAnimationDuration * (90 / 100);
    animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
    animateStrokeEnd.toValue   = [NSNumber numberWithFloat:1.0f];
    [progressLayer addAnimation:animateStrokeEnd forKey:[NSString stringWithFormat:@"animateStronke%d", number]];
}

1 个答案:

答案 0 :(得分:0)

该修补程序是:

[self drawArcNo:0 withProgress:progressRatio -= (CGFloat)(1 / 3)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self drawArcNo:1 withProgress:progressRatio -= (CGFloat)(1 / 3)];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self drawArcNo:2 withProgress:progressRatio -= (CGFloat)(1 / 3)];
    });
});

但我真的不喜欢这种灵魂。使用动画beginTime似乎很成问题。

更新: 感谢DavidRönnqvist,我修复了它:

animateStrokeEnd.fillMode = kCAFillModeBackwards;