我有一个包含 8 个 CAShapeLayer 对象的 NSArray。我的目标是在一定的持续时间内将填充颜色从一种填充到另一种,然后再次调用相同的方法。它似乎几乎有效,但没有考虑持续时间。这是代码,也许你可以帮忙?
[CATransaction begin];
for (int i = 0; i < MAX_NUM_LIGHTS; i++)
{
NSUInteger c = [colorsArray count];
CAShapeLayer *l = (CAShapeLayer*)llayers[i];
// the fill color after the anim is complete
[l setFillColor:((UIColor*)colorsArray[(i+index)%c]).CGColor];
CABasicAnimation *fillColorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"];
fillColorAnimation.duration = 2.0f;
fillColorAnimation.toValue = (id)((UIColor*)colorsArray[(i+index)%c]).CGColor;
fillColorAnimation.repeatCount = 0;
fillColorAnimation.autoreverses = FALSE;
[l addAnimation:fillColorAnimation forKey:@"fillColor"];
}
[CATransaction setCompletionBlock:^
{
if (currEffect != LpyEffect_MultiBall) return;
NSUInteger c = [colorsArray count];
int r = (index+1)%c;
[self simMultiBallEffect:r]; // recursive call, new index
}];
[CATransaction commit];
最佳答案
+[CATransaction setCompletionBlock:]
的文档说:
The completion block object that is guaranteed to be called (on the main thread) as soon as all animations subsequently added by this transaction group have completed (or have been removed.) If no animations are added before the current transaction group is committed (or the completion block is set to a different value,) the block will be invoked immediately.
添加所有动画后,您将设置完成 block 。您需要在添加任何动画之前对其进行设置。
关于ios - CAShapeLayer 动画的完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25753943/