I just wrote this code for you. I used CAKeyframeAnimation both because it allows multiple toValues , and because it allows more control over the animation.
//Set up layer and add it to view CALayer *layer = [CALayer layer]; layer.frame = self.view.bounds; [self.view.layer addSublayer:layer]; //Create animation CAKeyframeAnimation *colorsAnimation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"]; colorsAnimation.values = [NSArray arrayWithObjects: (id)[UIColor greenColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor redColor].CGColor, nil]; colorsAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.25], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:0.75],[NSNumber numberWithFloat:1.0], nil]; colorsAnimation.calculationMode = kCAAnimationPaced; colorsAnimation.removedOnCompletion = NO; colorsAnimation.fillMode = kCAFillModeForwards; colorsAnimation.duration = 3.0f; //Add animation [layer addAnimation:colorsAnimation forKey:nil];
source share