3
我试图实现一个循环指标,随着值的变化绘制动画本身。我使用CAShapeLayer
来绘制动画。到目前为止,我每次都从头开始吸收它,它按预期工作。现在我想让它变得更加圆滑,并且将它拉到前面并根据新值是大于还是小于前一个值来“擦除”。我不知道如何实现删除部分。有背景,所以我不能用白色在上面画。这是一张图片,可以更好地了解它的外观。iOS的“擦除”与动画CAShapeLayer绘图
任何想法擦除部如何才能实现?对于类似的问题,这里有一些解决方案,但那些不涉及动画。
我用这个代码绘制:
- (void)drawCircleAnimatedWithStartAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
color:(UIColor *)color
radius:(int)radius
lineWidth:(int)lineWidth {
CAShapeLayer *circle = [CAShapeLayer layer];
circle.name = @"circleLayer";
circle.path = ([UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius-1 startAngle:startAngle endAngle:endAngle clockwise:YES].CGPath);
// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor colorWithRed:19.0/255 green:167.0/255 blue:191.0/255 alpha:1].CGColor;
circle.lineWidth = lineWidth;
// Add to parent layer
for (CALayer *layer in self.circleView.layer.sublayers) {
if ([layer.name isEqualToString:@"circleLayer"]) {
[layer removeFromSuperlayer];
break;
}
}
[self.circleView.layer addSublayer:circle];
circle.zPosition = 1;
// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 0.5;
drawAnimation.repeatCount = 1.0; // Animate only once..
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
这是一个真棒解决方案,非常感谢你!表现层与这样一个不错的接触!你救了我几个小时的头痛。所以干净和容易实施:) – NKorotkov 2014-11-06 09:10:25