Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Natasha Judd
Natasha Judd
5,606 Points

IOS: Image Based animation - Crystal Ball not animating

Hi everyone,

I'm following along with developing the crystal ball app and I've just finished the image based animation video, but I'm not seeing the animation when I run the simulator. Can anyone see any errors in my code below?

Thanks for your help, Natasha

Here's my viewDidLoad

- (void)viewDidLoad

{ [super viewDidLoad];

UIImage *image = [UIImage imageNamed:@"background.png" ];

self.imageView = [[UIImageView alloc] initWithImage:image];

[self.view insertSubview:self.imageView atIndex:0];

    self.imageView.animationImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"cball00001"], [UIImage imageNamed:@"cball00002"], [UIImage imageNamed:@"cball00003"], [UIImage imageNamed:@"cball00004"], [UIImage imageNamed:@"cball00005"], [UIImage imageNamed:@"cball00006"], [UIImage imageNamed:@"cball00007"], [UIImage imageNamed:@"cball00008"], [UIImage imageNamed:@"cball00009"], [UIImage imageNamed:@"cball00010"], [UIImage imageNamed:@"cball00011"], [UIImage imageNamed:@"cball00012"], [UIImage imageNamed:@"cball00013"], [UIImage imageNamed:@"cball00014"], [UIImage imageNamed:@"cball00015"], [UIImage imageNamed:@"cball00016"], [UIImage imageNamed:@"cball00017"], [UIImage imageNamed:@"cball00018"], [UIImage imageNamed:@"cball00019"], [UIImage imageNamed:@"cball00020"], [UIImage imageNamed:@"cball00021"],[UIImage imageNamed:@"cball00022"], [UIImage imageNamed:@"cball00023"], [UIImage imageNamed:@"cball00024"], nil];
self.imageView.animationDuration = 1.0;
self.imageView.animationRepeatCount = 1;


self.predictionArray = [[NSArray alloc] initWithObjects:@"It is certain",@"It is decidely so",@"All signs say YES",@"The stars are not aligned",@"My reply is no",@"It is doubtful",@"Better not tell you now",@"Concentrate and ask again",@"Unable to answer now", nil];

}

Here's what I've got in makePrediction

- (void) makePrediction {

NSUInteger index = arc4random_uniform(self.predictionArray.count);
self.predictionLabel.text = [self.predictionArray objectAtIndex:index];

[self.imageView startAnimating];

}

12 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

So the issue is that you have an ImageView in your Storyboard as well as one that you are creating programmatically. If you delete the one in your Storyboard then you can see that the animation works. You should be using one or the other not both.

If you are curious on how to make the app work on the taller iPhone 5 screen then check out this discussion: http://teamtreehouse.com/forum/programming-a-background-image-for-iphone-5

Thanks, this same issue was driving me nuts!

Natasha Judd
Natasha Judd
5,606 Points

Thanks very much for your help. I've taken off the Storyboard image view and my crystal ball jumps and spins on command.

Will take a look at the background issue too.

Very excited to see this coming together :)

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Your code seems fine. Have you added the images to your project? Is your makePrediction being called when you click on the button? You can add an NSLog within the makePrediction method to ensure it is being called.

Natasha Judd
Natasha Judd
5,606 Points

Hi Amit,

Thanks for getting back to me.

I've added NSLog(@"Working"); to the makePrediction method and it's being called (I get a 'Working' in the log file) when I click on the screen (I've taken off the button) or simulate a shake.

All 24 of the png files are in the CrystalBall -> Images -> Animation folder.

Anything else I should check? Could it be that the animation is happening behind the main image?

Natasha

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Can you share your project (zip it up and share it through Dropbox)? The code looks fine so I'm not sure what else it could be.

Natasha Judd
Natasha Judd
5,606 Points

Thanks Amit, I've added it to a DropBox folder here: https://www.dropbox.com/sh/03j00hf99xfghy0/lgE4x8wK8M

Hope I've done that all right - I'm still getting used to the Mac way of doing things.

Good little discussion. @Amit I just have another question because you said "You should be using one or the other not both." Is one better than the other? Say for setting a background image... is there any real benefit of doing this in the interface builder? I guess the time/processing you save is negligible.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Ernest didn't mean to suggest that one is better than the other. It was just in this instance. She had two image views where the one created using the Storyboard was hiding the one created programmatically. Either way is fine. I personally prefer to create views using Storyboards as much as possible so that my controller code does not contain an view code. The code ends up being maintainable and readable.

Okay, just triple checking!

Timothy Chan
Timothy Chan
3,715 Points

Sweet. Thanks, this thread helped me too. I did the same thing...had 2 Imageviews. Now everything is working happily.

Great thread! I was in this exact same position and could not get it to work. Thanks everyone

Mine will not animate and I do not see myself creating two instances of the image. It displays CB00001 but will not animate and i know makePrediction is being called because the prediction is changing.