ios - CAShapeLayer 动画的完成 block

标签 ios objective-c core-animation calayer

我有一个包含 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/

相关文章:

css - iOS 上带有 Phonegap 的多个 WebView

iphone - 如何检测应用程序是否在模拟器或设备上运行

ios - 为 iphone 4s 和 iphone 5 编写 cocos2d 代码

iphone - 将 nil 传递给 performSelector

iphone - 在 iPhone 模拟器上点击菜单按钮然后重新启动应用程序后,有没有办法查看日志记录?

ios - "[NSBigMutableString substringWithRange:]: Range {0, 10} out of bounds; string length 9"撤消错误

objective-c - FinderSync 扩展 - 从不调用 requestBadgeIdentifierForURL

ios - 使用淡入淡出效果动画 CALayer mask 变化

ios - 从中心变换 UIView

ios - 从圆形或 donut 中绘制线段