我写了“空格”分隔的进度圈,在其中按顺序对进度进行动画处理。
方法以不同的进度和弧号被调用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/