在多个对象上分组动画

时间:2013-08-10 12:34:57

标签: ios animation cabasicanimation cakeyframeanimation

我正在尝试为机器中的许多气泡制作动画。我正在使用基本动画将气泡从小气泡缩放到大气泡,并使用关键帧动画将气泡以曲线形式发送到屏幕上。这些组合成一组动画。

我已经仔细阅读了所有可以尝试使其工作的内容,但我有以下两个问题。

  1. 我无法让缩放动画与关键帧一起工作 当我尝试为每个气泡制作动画时的动画。
  2. 动画同时发生在他们身上。我想看看他们的动画片 在另一个之后,但它们都在一起动画。
  3. 这是我的动画代码。

    -(void)EmitBubbles:(UIButton*)bubblename :(CGRect)RectPassed
    {
    bubblename.hidden = NO;
     // Set position and size of each bubble to be at head of bubble machine and size (0,0)
    CGPoint point = (RectPassed.origin);
    CGRect ButtonFrame;
    ButtonFrame = bubblename.bounds;
    ButtonFrame.size = CGSizeMake(0, 0);
    bubblename.bounds = ButtonFrame;
    CGRect OldButtonBounds = bubblename.bounds;
    CGRect NewButtonBounds = OldButtonBounds;
    NewButtonBounds.size = RectPassed.size;
    
    CGFloat animationDuration = 0.8f;
    
    // Set up scaling
    CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
    resizeAnimation.fillMode = kCAFillModeForwards;
    resizeAnimation.duration = animationDuration;
    resizeAnimation.fromValue = [NSValue valueWithCGSize:OldButtonBounds.size]; 
    resizeAnimation.toValue = [NSValue valueWithCGSize:NewButtonBounds.size]; 
    
    // Set Actual bubble size after animation 
    bubblename.bounds = NewButtonBounds;
    
    // Setup Path
    CGPoint MidPoint = CGPointMake(500,50);
    CGPoint EndPoint = CGPointMake(RectPassed.origin.x, RectPassed.origin.y); 
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, nil, bubblename.bounds.origin.x, bubblename.bounds.origin.y);
    CGPoint NewLoc = CGPointMake(745, 200);
    CGPathMoveToPoint(curvedPath, NULL, NewLoc.x,NewLoc.y); 
    CGPathAddCurveToPoint(curvedPath, NULL, 745,200,MidPoint.x,MidPoint.y, EndPoint.x, EndPoint.y);
    pathAnimation.path = curvedPath;
    
    // Set Bubble action position after animation
    bubblename.layer.position = point;
    
    // Add scale and path to animation group
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.removedOnCompletion = YES;
    [group setAnimations:[NSArray arrayWithObjects:pathAnimation, resizeAnimation, nil]];
    group.duration = animationDuration;
    
    [bubblename.layer addAnimation:group forKey:@"nil"]; //savingAnimation
    
    
    CGPathRelease(curvedPath);
    

    }

    我使用

    为每个气泡调用动画
    [self EmitBubbles:btnbubble1 :CGRectMake(200, 500, 84, 88)];
    [self EmitBubbles:btnbubble2 :CGRectMake(200, 500, 84, 88)];
    

    屏幕上有20个气泡。传递的rect的大小是我想要气泡的最终大小。

    我是否可以使用比例和关键帧路径分别为每个气泡设置动画,而无需为每个气泡编写上述代码?

    由于

1 个答案:

答案 0 :(得分:0)

行。我想我做到了。经过几个小时的环视,看看Stackoverflow和其他地方,我发现这些物体一个接一个地生动。我必须根据应用时间来计算时间。我用了。

group.beginTime = CACurrentMediaTime()+ AnimationDelay

在继续动画之前,暂停动画暂时出现在最终位置。我不得不使用

group.fillMode = kCAFillModeBackwards;