iphoneobjective-cioscore-animationcabasicanimation

running CABasicAnimation sequentially


How can I have one CABasicAnimation to run after the other one has finishes? In other words sequentially. I've added the begin time of the second animation, however it seems that the second animation doesn't gets executed:

CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    appearance.duration = 0.5;
    appearance.fromValue = [NSNumber numberWithFloat:0];
    appearance.toValue = [NSNumber numberWithFloat:340];
    appearance.repeatCount = 1;
    appearance.fillMode = kCAFillModeForwards;
    appearance.removedOnCompletion = NO;
    [notif.layer addAnimation:appearance forKey:@"transform.translation.y"];



    CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    theAnimation.duration = 0.5;
    theAnimation.fromValue = [NSNumber numberWithFloat:0];
    theAnimation.toValue = [NSNumber numberWithFloat:10];
    theAnimation.repeatCount = 3;
    theAnimation.autoreverses = YES;
    theAnimation.fillMode = kCAFillModeForwards;
    theAnimation.removedOnCompletion = NO;
    theAnimation.beginTime = appearance.beginTime + appearance.duration;
    [notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];

Any idea why?


Solution

  • Updated Code, this will work!

    1st animation

    CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        [appearance setValue:@"animation1" forKey:@"id"];
        appearance.delegate = self;
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
        appearance.duration = 0.5;
        appearance.fromValue = [NSNumber numberWithFloat:0];
        appearance.toValue = [NSNumber numberWithFloat:340];
        appearance.repeatCount = 1;
        appearance.fillMode = kCAFillModeForwards;
        appearance.removedOnCompletion = NO;
        [notif.layer addAnimation:appearance forKey:@"transform.translation.y"];
    

    2nd Animation:

        - (void)animationDidStop:(CAAnimation *)theAnimation2 finished:(BOOL)flag {
        if([[theAnimation2 valueForKey:@"id"] isEqual:@"animation1"]) {
        CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        theAnimation.duration = 0.5;
        theAnimation.fromValue = [NSNumber numberWithFloat:0];
        theAnimation.toValue = [NSNumber numberWithFloat:10];
        theAnimation.repeatCount = 3;
        theAnimation.autoreverses = YES;
        theAnimation.fillMode = kCAFillModeForwards;
        theAnimation.removedOnCompletion = NO;
        theAnimation.beginTime = appearance.beginTime + appearance.duration;
        [notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];
    }
    

    }

    This is how the 2nd animation would fire after the 1st one finishes.