核心animation图像序列

我将如何去创build一个核心animation的图像序列。 我想要:

添加image1 1秒,然后删除图像

添加image2 2秒,然后删除图像

添加image1 3秒,然后删除

CGImageRef image1 = [self getImage1]; CALayer *image1Layer = [CALayer layer]; image1Layer.bounds = CGRectMake(0, 0, 480, 320); image1Layer.position = CGPointMake(0, 0); image1Layer.contents = (id)image1; CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"animation"]; animation1.repeatCount = 0; animation1.duration = 2.0; animation1.removedOnCompletion = YES; // i would like to remove image here animation1.beginTime = AVCoreAnimationBeginTimeAtZero; [image1Layer addAnimation:animation1 forKey:nil]; 

上面的代码添加了一个图像,但不删除它。

干杯

最简单的方法是使用CABasicAnimation作为内容键:

 CABasicAnimation *animation = [CABasicAnimation animation]; animation.fromValue = (id)[UIImage imageNamed:@"image1.png"].CGImage; animation.toValue = (id)[UIImage imageNamed:@"image2.png"].CGImage; animation.duration = 1.0f; animation.repeatCount = HUGE_VAL; // animation.autoreverses = YES; [image1Layer addAnimation:animation forKey:@"contents"]; 

这个animation将无限地改变image1和image2之间的图层内容。 您可能需要设置autoreverses属性以实现更平滑的过渡 – 以任何方式testinganimation并select您最喜欢的选项。