核心动画图像序列

时间:2010-10-18 12:09:13

标签: ios iphone image core-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];

上面的代码添加了一个图片,但没有将其删除。

干杯

2 个答案:

答案 0 :(得分:8)

最简单的方法是将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属性以获得更平滑的过渡 - 以任何方式测试动画并选择您最喜欢的选项。

答案 1 :(得分:0)

您也可以通过使用 CAKeyframeAnimation 来实现这一点。请按照以下步骤操作:

  1. 创建一个 UIImage 数组,并添加图像
  2. 准备 CAKeyframeAnimation 动画
  3. 准备CALayer
  4. 为图层添加动画

Reference