我将如何使用核心动画创建图像序列。我想:
添加 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"];
此动画将无限更改 image1 和 image2 之间的图层内容。您可能希望设置
autoreverses
属性以获得更平滑的过渡 - 以任何一种方式测试动画并选择您最喜欢的选项。