1

我正在尝试使用 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];

    }

}
4

0 回答 0