При анимации CAShapeLayer необходимо постоянно обновлять путь к слою или есть ли способ полагаться только на CABasicAnimation?CAShapeLayer Анимация без обновления пути
В приведенном ниже примере я установил четыре круга и нарисовал их. Тогда я хочу оживить их, чтобы исчезнуть. Однако после того, как анимация завершилась, они возвращаются к исходным дорожкам.
int radius = halfWidth-30; //the radius is the distance out from the centre
trackActive.path = [UIBezierPath bezierPathWithArcCenter:cicleCenter radius:radius startAngle:degreesToRadians(circleStart) endAngle:degreesToRadians(circleEnd) clockwise:true].CGPath;
circleActive.path = [UIBezierPath bezierPathWithArcCenter:cicleCenter radius:radius startAngle:degreesToRadians(circleStart) endAngle:degreesToRadians(circleEnd) clockwise:true].CGPath;
radius = halfWidth - 10;
trackNew.path = [UIBezierPath bezierPathWithArcCenter:cicleCenter radius:radius startAngle:degreesToRadians(circleStart) endAngle:degreesToRadians(circleEnd) clockwise:true].CGPath;
circleNew.path = [UIBezierPath bezierPathWithArcCenter:cicleCenter radius:radius startAngle:degreesToRadians(circleStart) endAngle:degreesToRadians(circleEnd) clockwise:true].CGPath;
NSArray * layers = @[trackActive, circleActive, trackNew, circleNew];
for (CAShapeLayer * layer in layers){
[CATransaction begin];
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 1.0f;
animation.removedOnCompletion = false;
animation.fromValue = @(1.0);
animation.toValue = @(0.0);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[layer addAnimation:animation forKey:@"circleAnimation"];
[CATransaction commit];
}
Я понимаю, что я могу добавить к этому, чтобы установить новый путь:
[CATransaction setCompletionBlock:^{
//set the new path
layer.path = [UIBezierPath bezierPathWithArcCenter:cicleCenter radius:radius startAngle:degreesToRadians(circleStart) endAngle:degreesToRadians(arcEnd) clockwise:true].CGPath;
}];
Но предпочел бы избежать необходимости держать обновление пути, и вместо того, чтобы полагаться на анимацию слое исключительно. Это возможно?
Нечто вроде: layer.path = animation.path или animation.updatesOriginalPath = true; может быть желаемое за действительное, не уверен.
Вы можете опустить эффект пружины, установив strokeEnd слоя на ноль, поскольку анимация не изменяет состояние слоя. – clemens