Apple TV上的CAKeyframeAnimation未显示

时间:2016-03-01 17:42:21

标签: ios core-animation apple-tv

我正试图一个接一个地以电影书的形式为一系列图像制作动画。当我在Apple TV模拟器上运行下面的代码时,我看到一个空白的白色透明屏幕是默认的。主视图和我的图像视图似乎消失了。有什么建议可以吗?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor blackColor];
    self.imageview1 =[[UIImageView alloc] initWithFrame:CGRectMake(0,0,480,1080)];
    self.imageview1.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.imageview1];
    self.array1 = [NSMutableArray array];

    for(int i = 0; i <10; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"loop_00_%d",i];
        //NSLog(imageName);
        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [self.array1 addObject:image];
    }

    [self animateImages];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)animateImages
{
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    keyframeAnimation.values = self.array1;  // array with images

    keyframeAnimation.repeatCount = 1.0f;
    keyframeAnimation.duration = 10.0;

    keyframeAnimation.removedOnCompletion = YES;

    CALayer *layer = self.imageview1.layer;

    [layer addAnimation:keyframeAnimation
                 forKey:@"flingAnimation"];
}

1 个答案:

答案 0 :(得分:0)

问题是CAKeyframeAnimation只接受CGImages,而不是UIImages并且无声地失败。需要将CGImages添加到数组并进行如下转换:

UIImage *image = [UIImage imageWithContentsOfFile:path];
[self.array1 addObject:(id)image.CGImage];