ios - 如何在CABasicAnimation启动之前防止CAShapeLayer绘制

标签 ios objective-c cabasicanimation cashapelayer

我写了“空格”分隔的进度圈,在其中按顺序对进度进行动画处理。

方法以不同的进度和弧号被调用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]];
}

最佳答案

修复程序是:

[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;

关于ios - 如何在CABasicAnimation启动之前防止CAShapeLayer绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932572/

相关文章:

iphone - NSTimer 立即触发

ios - CABasicAnimation - 从一个动画切换到另一个。如何获取动画的currentValue?

iOS动画绘制多个圆,只显示一个

使用 VoiceOver 时不调用 JavaScript 函数

ios - 具有可达性的旋转网络图标

objective-c - NSTreeController/NSOutlineView失去选择

objective-c - 当应用程序进入后台时执行 Segue

ios - SceneKit 中的连续变形动画

iOS如何访问私有(private)文件进行共享扩展

iphone - 如何使用 DISPATCH_QUEUE_CONCURRENT 和屏障 block 复制 FIFO 队列?