在循环和CAAnimationGroup中使用CAKeyframeAnimation为视图(图层)设置动画

时间:2013-03-13 11:42:28

标签: ios animation view timing cakeyframeanimation

我正在尝试使用CAKeyframeAnimation和CAAnimationGroup为几个视图(图层)设置动画。要设置动画的每个视图都包含在一个数组中。我的想法是循环遍历数组,其中每个视图的CAKeyframeAnimation实例将被添加到动画数组中,然后将其添加到CAAnimationGroup。我的目标是一个接一个地为每个视图制作动画。据我所知,我需要使用该组才能获得beginTime的参考。

我可能遗漏了一些明显的东西,但我的代码不起作用

CAAnimationGroup *cardAnimationGroup = [CAAnimationGroup animation];
cardAnimationGroup.delegate = self;
cardAnimationGroup.removedOnCompletion = NO;
cardAnimationGroup.beginTime = 0.0f;
[cardAnimationGroup setValue:@"animation0" forKey:@"id"];
cardAnimationGroup.duration = 1.2f * [_myViewsArray count];

NSMutableArray *animationsArray = [[NSMutableArray alloc] init];


// Loop through views
for (UIView *cardView in _myViewsArray)
{

    NSUInteger index = [_myViewsArray indexOfObject:cardView];
    // my target origin
    CGFloat yOffset = (-(cardView.frame.origin.y - _middleCardView.frame.origin.y) - index * 2);

    CAKeyframeAnimation *translateCardPositionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    translateCardPositionAnimation.removedOnCompletion = YES;
    //translateCardPositionAnimation.fillMode = kCAFillModeForwards;
    translateCardPositionAnimation.beginTime = 1.2f * index;
    translateCardPositionAnimation.duration = 1.2f;

    translateCardPositionAnimation.values = [NSArray arrayWithObjects:
                                                   [NSNumber numberWithFloat:0.0f],
                                                   [NSNumber numberWithFloat:yOffset],
                                                   [NSNumber numberWithFloat:yOffset],nil];

    translateCardPositionAnimation.keyTimes = [NSArray arrayWithObjects:
                                                     [NSNumber numberWithFloat:0.0f],
                                                     [NSNumber numberWithFloat:1.0f],
                                                     [NSNumber numberWithFloat:1.2f], nil];


    [_containerCardView addSubview:cardView];
    [_containerCardView sendSubviewToBack:cardView];

    [animationsArray addObject:translateCardPositionAnimation];
    [cardView.layer addObject:translateCardPositionAnimation forKey:@"id"];

    if (index == [_myViewsArray count]-1) {
        [cardAnimationGroup setAnimations:animationsArray];

    }

}

0 个答案:

没有答案