Animacja CAShapeLayer (łuk od 0 do ostatecznego rozmiaru)
mamCAShapeLayer
w którym dodaje się łuk za pomocąUIBezierPath
. Widziałem kilka postów (jeden faktycznie) tutaj na stackoverflow, ale nikt nie dał mi odpowiedzi. Jak już wspomniałem w tytule, chciałbym animować łuk (tak będzie na wykresie kołowym).
W jaki sposób można wykonać animację od „pustego” łuku do pełnego rozszerzenia? Tak jak zakrzywiony pasek postępu. Czy to naprawdę niemożliwe, aby to zrobićCoreAnimation
?
Oto jak „robię” łuk. Oh i zignoruj komentarze i obliczenia, ponieważ jestem przyzwyczajony do przeciwnego do ruchu wskazówek zegara okręgu i jabłko idzie zgodnie z ruchem wskazówek zegara. Łuki wyglądają dobrze, potrzebuję tylko animacji:
//Apple's unit circle goes in clockwise rotation while im used to counter clockwise that's why my angles are mirrored along x-axis
workAngle = 6.283185307 - DEGREES_TO_RADIANS(360 * item.value / total);
//My unit circle, that's why negative currentAngle
[path moveToPoint:CGPointMake(center.x + cosf(outerPaddingAngle - currentAngle) * (bounds.size.width / 2), center.y - (sinf(outerPaddingAngle - currentAngle) * (bounds.size.height / 2)))];
//Apple's unit circle, clockwise rotation, we add the angles
[path addArcWithCenter:center radius:120 startAngle:currentAngle endAngle:currentAngle + workAngle clockwise:NO];
//My unit circle, counter clockwise rotation, we subtract the angles
[path addLineToPoint:CGPointMake(center.x + cosf(-currentAngle - workAngle) * ((bounds.size.width / 2) - pieWidth), center.y - sinf(-currentAngle - workAngle) * ((bounds.size.height / 2) - pieWidth))];
//Apple's unit circle, clockwise rotation, addition of angles
[path addArcWithCenter:center radius:120 - pieWidth startAngle:currentAngle + workAngle endAngle:currentAngle - innerPaddingAngle clockwise:YES];
//No need for my custom calculations since I can simply close the path
[path closePath];
shape = [CAShapeLayer layer];
shape.frame = self.bounds;
shape.path = path.CGPath;
shape.fillColor = kRGBAColor(255, 255, 255, 0.2f + 0.1f * (i + 1)).CGColor; //kRGBAColor is a #defined
[self.layer addSublayer:shape];
//THIS IS THE PART IM INTERESTED IN
if (_shouldAnimate)
{
CABasicAnimation *pieAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pieAnimation.duration = 1.0;
pieAnimation.removedOnCompletion = NO;
pieAnimation.fillMode = kCAFillModeForwards;
//from and to are just dummies so I dont get errors
pieAnimation.fromValue = [NSNumber numberWithInt:0]; //from what
pieAnimation.toValue = [NSNumber numberWithFloat:1.0f]; //to what
[shape addAnimation:pieAnimation forKey:@"animatePath"];
}