views:

20

answers:

1

I'm sure this is simple but I am trying to add an array of images to a layer. Here is what I have so far:

// Create the fish layer
 fishLayer = [CALayer layer];
 //fish  = [UIImageView imageNamed:@"Fish.png"];

 fish.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"swim01.png"],
        [UIImage imageNamed:@"swim02.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim06.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim02.png"], nil];

 fish.animationDuration = 1.50;
 fish.animationRepeatCount = -1;
 [fish startAnimating];


 //[self.view addSubview:fish];
 //This should add the animated array to layer.
 fishLayer.contents = fish;

 fishLayer.bounds = CGRectMake(0, 0, 56, 56);
 fishLayer.position = CGPointMake(self.view.bounds.size.height / 2,
         self.view.bounds.size.width / 2);

[self.view.layer addSublayer:fishLayer];

There is no error but the array of images don't appear on the screen. I think maybe this line is the probem..

fishLayer.contents = fish;

I have added the imageview to my header files and added it in the XIB

Please help if you can,

Cheers,

Adam

A: 

Reading your code, it looks like you're not initializing fish. Since you're not getting any errors, I assume it is an instance variable, which means it gets set to nil initially. So when you set fish.animationImages, you essentially do nothing (fish is nil). Same with every other use of fish in this code snippet.

It looks like you were using views initially, but then commented all that out. Why are you trying to use a layer? You should be able to just do this:

fish = [UIImageView imageNamed:@"Fish.png"];

fish.animationImages = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"swim01.png"],
        [UIImage imageNamed:@"swim02.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim06.png"],
        [UIImage imageNamed:@"swim05.png"],
        [UIImage imageNamed:@"swim04.png"],
        [UIImage imageNamed:@"swim03.png"],
        [UIImage imageNamed:@"swim02.png"], nil];

fish.animationDuration = 1.50;
fish.animationRepeatCount = -1;
[fish startAnimating];

[self.view addSubview:fish];

fish.bounds = CGRectMake(0, 0, 56, 56);
fish.position = CGPointMake(self.view.bounds.size.height / 2,
         self.view.bounds.size.width / 2);
Hilton Campbell