【发布时间】:2014-03-20 01:29:29
【问题描述】:
示例项目: http://cl.ly/1W3V3b0D2001
我正在使用CABasicAnimation 创建一个类似于饼图的进度指示器。类似于 iOS 7 应用下载动画:
动画设置如下:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGFloat radius = CGRectGetWidth(self.frame) / 2;
CGFloat inset = 1;
CAShapeLayer *ring = [CAShapeLayer layer];
ring.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset)
cornerRadius:radius-inset].CGPath;
ring.fillColor = [UIColor clearColor].CGColor;
ring.strokeColor = [UIColor whiteColor].CGColor;
ring.lineWidth = 2;
self.innerPie = [CAShapeLayer layer];
inset = radius/2;
self.innerPie.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset)
cornerRadius:radius-inset].CGPath;
self.innerPie.fillColor = [UIColor clearColor].CGColor;
self.innerPie.strokeColor = [UIColor whiteColor].CGColor;
self.innerPie.lineWidth = (radius-inset)*2;
self.innerPie.strokeStart = 0;
self.innerPie.strokeEnd = 0;
[self.layer addSublayer:ring];
[self.layer addSublayer:self.innerPie];
self.progress = 0.0;
}
通过设置视图的进度来触发动画:
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
self.progress = progress;
if (animated) {
CGFloat totalDurationForFullCircleAnimation = 0.25;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
self.innerPie.strokeEnd = progress;
pathAnimation.delegate = self;
pathAnimation.fromValue = @([self.innerPie.presentationLayer strokeEnd]);
pathAnimation.toValue = @(progress);
pathAnimation.duration = totalDurationForFullCircleAnimation * ([pathAnimation.toValue floatValue] - [pathAnimation.fromValue floatValue]);
[self.innerPie addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}
else {
[CATransaction setDisableActions:YES];
[CATransaction begin];
self.innerPie.strokeEnd = progress;
[CATransaction commit];
}
}
但是,如果我将进度设置为较小的值,例如 0.25,动画中会出现跳跃。它顺时针向前一点,然后向后跳,然后像往常一样继续向前。 如果将持续时间或进度设置得更高,则不会发生这种情况,这毫无价值。
如何停止跳跃?此代码在所有情况下都运行良好,除非进度非常低。我做错了什么?
【问题讨论】:
-
不要在
begin和commit之外配置事务。你最终可能会影响其他事情。
标签: ios objective-c uiview core-animation cabasicanimation