Search code examples
ioscore-animationapple-tv

CAKeyframeAnimation on Apple TV not showing


I'm trying to animate a series of images in flick book style one after another. When I run the code below on Apple TV simulator I see a blank, white transparent screen which is default. The main view and my image view seem to disappear. Any suggestions why please?

- (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"];
}

Solution

  • The problem was CAKeyframeAnimation only accepts CGImages, not UIImages and fails silently. Needed to add CGImages to the array and cast as below:

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