使用CAAnimationGroup进行序列动画

And*_*rea 9 cocoa cocoa-touch core-animation objective-c ios

我正在尝试制作一系列动画,我在CAAnimationGroup正确的类中找到了实现该对象.在实践中,我添加了一个视图不同的子视图,我想用弹跳效果动画他们的条目,事实是我想看到他们的动画发生在前一个完成后.我知道我可以设置代表,但我认为这CAAnimationGroup是正确的选择.
后来我发现组动画只能属于一个图层,但我需要它在屏幕上的不同图层上.当然在托管层上不起作用.一些建议?

- (void) didMoveToSuperview {
    [super didMoveToSuperview];
    float startTime = 0;
    NSMutableArray * animArray = @[].mutableCopy;
    for (int i = 1; i<=_score; i++) {
        NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: self.greenLeaf];
        UIImageView * greenLeafImageView = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
        greenLeafImageView.image = [UIImage imageNamed:@"greenLeaf"];
        CGPoint leafCenter =  calculatePointCoordinateWithRadiusAndRotation(63, -(M_PI/11 * i) - M_PI_2);
        greenLeafImageView.center = CGPointApplyAffineTransform(leafCenter, CGAffineTransformMakeTranslation(self.bounds.size.width/2, self.bounds.size.height));
        [self addSubview:greenLeafImageView];

        //Animation creation
        CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        greenLeafImageView.layer.transform = CATransform3DIdentity;
        bounceAnimation.values = @[
                                  [NSNumber numberWithFloat:0.5],
                                  [NSNumber numberWithFloat:1.1],
                                  [NSNumber numberWithFloat:0.8],
                                  [NSNumber numberWithFloat:1.0]
                                  ];

        bounceAnimation.duration = 2;
        bounceAnimation.beginTime = startTime;
        startTime += bounceAnimation.duration;

        [animArray addObject:bounceAnimation];
        //[greenLeafImageView.layer addAnimation:bounceAnimation forKey:nil];


    }
    // Rotation animation
    [UIView animateWithDuration:1 animations:^{
        self.arrow.transform = CGAffineTransformMakeRotation(M_PI/11 * _score);
    }];
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.animations = animArray;
    group.duration = [[ animArray valueForKeyPath:@"@sum.duration"] floatValue];
    [self.layer addAnimation:group forKey:nil];
}
Run Code Online (Sandbox Code Playgroud)

Can*_*Can 23

CAAnimationGroup用于将多个CAAnimation子类堆叠在一起以形成动画,例如,一个动画可以执行比例,另一个动画可以移动它,而第三个可以旋转它,它不是用于管理多个层,而是用于具有多个覆盖动画.

这就是说,我认为解决您的问题最简单的方法,就是给每个分配CAAnimation一个beginTime相当于之前所有的的持续时间,来说明总和:

for i in 0 ..< 20
{
    let view : UIView = // Obtain/create the view...;
    let bounce = CAKeyframeAnimation(keyPath: "transform.scale")

    bounce.duration  = 0.5;
    bounce.beginTime = CACurrentMediaTime() + bounce.duration * CFTimeInterval(i);

    // ...

    view.layer.addAnimation(bounce, forKey:"anim.bounce")
}
Run Code Online (Sandbox Code Playgroud)

请注意,每个人都会获得duration * i,并且CACurrentMediaTime()在使用beginTime属性时这是必需的(它基本上是"现在"的高精度时间戳,用于动画).整行可以解释为now + duration * i.

必须注意CAAnimation的是,如果将s添加到a CAAnimationGroup,则其beginTime变为相对于组的开始时间,因此5.0动画的值将是5.0整个组开始后的秒数.在这种情况下,您不使用CACurrentMediaTime()