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

Jon Reese
Jon Reese
2,335 Points

Crystal Ball Animation Not Working

Thought I had everything how Amit said to code, but it's not working

#import "THViewController.h"
#import "THCrystalBall.h"

@interface THViewController ()

@end

@implementation THViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.crystalBall = [[THCrystalBall alloc] init];

    self.backgroundImageView.animationImages = [[NSArray alloc] initWithObjects:

    [UIImage imageNamed:@"CB00001"],
    [UIImage imageNamed:@"CB00002"],
    [UIImage imageNamed:@"CB00003"],
    [UIImage imageNamed:@"CB00004"],
    [UIImage imageNamed:@"CB00005"],
    [UIImage imageNamed:@"CB00006"],
    [UIImage imageNamed:@"CB00007"],
    [UIImage imageNamed:@"CB00008"],
    [UIImage imageNamed:@"CB00009"],
    [UIImage imageNamed:@"CB00010"],
    [UIImage imageNamed:@"CB00011"],
    [UIImage imageNamed:@"CB00012"],
    [UIImage imageNamed:@"CB00013"],
    [UIImage imageNamed:@"CB00014"],
    [UIImage imageNamed:@"CB00015"],
    [UIImage imageNamed:@"CB00016"],
    [UIImage imageNamed:@"CB00017"],
    [UIImage imageNamed:@"CB00018"],
    [UIImage imageNamed:@"CB00019"],
    [UIImage imageNamed:@"CB00020"],
    [UIImage imageNamed:@"CB00021"],
    [UIImage imageNamed:@"CB00022"],
    [UIImage imageNamed:@"CB00023"],
    [UIImage imageNamed:@"CB00024"],
    [UIImage imageNamed:@"CB00025"],
    [UIImage imageNamed:@"CB00026"],
    [UIImage imageNamed:@"CB00027"],
    [UIImage imageNamed:@"CB00028"],
    [UIImage imageNamed:@"CB00029"],
    [UIImage imageNamed:@"CB00030"],
    [UIImage imageNamed:@"CB00031"],
    [UIImage imageNamed:@"CB00032"],
    [UIImage imageNamed:@"CB00033"],
    [UIImage imageNamed:@"CB00034"],
    [UIImage imageNamed:@"CB00035"],
    [UIImage imageNamed:@"CB00036"],
    [UIImage imageNamed:@"CB00037"],
    [UIImage imageNamed:@"CB00038"],
    [UIImage imageNamed:@"CB00039"],
    [UIImage imageNamed:@"CB00040"],
    [UIImage imageNamed:@"CB00041"],
    [UIImage imageNamed:@"CB00042"],
    [UIImage imageNamed:@"CB00043"],
    [UIImage imageNamed:@"CB00044"],
    [UIImage imageNamed:@"CB00045"],
    [UIImage imageNamed:@"CB00046"],
    [UIImage imageNamed:@"CB00047"],
    [UIImage imageNamed:@"CB00048"],
    [UIImage imageNamed:@"CB00049"],
    [UIImage imageNamed:@"CB00050"],
    [UIImage imageNamed:@"CB00051"],
    [UIImage imageNamed:@"CB00052"],
    [UIImage imageNamed:@"CB00053"],
    [UIImage imageNamed:@"CB00054"],
    [UIImage imageNamed:@"CB00055"],
    [UIImage imageNamed:@"CB00056"],
    [UIImage imageNamed:@"CB00057"],
    [UIImage imageNamed:@"CB00058"],
    [UIImage imageNamed:@"CB00059"],
    [UIImage imageNamed:@"CB00060"], nil];

    self.backgroundImageView.animationDuration = 2.5f;

    self.backgroundImageView.animationRepeatCount = 1;

}

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

    self.predictionLabel.text = [self.crystalBall randomPrediction];
}
- (IBAction)buttonPressed {
    self.predictionLabel.text = [self.crystalBall randomPrediction];
}
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    self.predictionLabel.text = nil;
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if  (motion == UIEventSubtypeMotionShake){
        self.predictionLabel.text = [self.crystalBall randomPrediction];
    }
}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"motion canceled");

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.predictionLabel.text = nil;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.predictionLabel.text = [self.crystalBall randomPrediction];
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches cancelled");
}
@end
Stone Preston
Stone Preston
42,016 Points

What happens when you run this code? errors? animation simply doesnt run?

Jon Reese
Jon Reese
2,335 Points

Animation just doesn't run, everything else seems to be fine

6 Answers

Stone Preston
Stone Preston
42,016 Points

is it possible you have two image views accidently added instead of just one and the animation is being hidden by the first non animated image view? open your view controller in your storyboard and check the document outline (its the little arrow button at the bottom left) expand your view controller, then your view, make sure you only have 1 image view.

Jon Reese
Jon Reese
2,335 Points

I only have one set to CB00001

Stone Preston
Stone Preston
42,016 Points

does your prediction change when you click/shake? And are you sure have included all the images in your project?

Jon Reese
Jon Reese
2,335 Points

Yes sir, button, touch, and shake all work. An odd behavior does happen though. Sometimes no prediction will come up. I thought it was an issue with my NSArray with a blank prediction, but it is not so. All images are present.

Stone Preston
Stone Preston
42,016 Points

Ahh, I think I found your issue. You are never calling your makePrediction method, which is what calls the startAnimating method. try adding a call to that method in your touches ended method

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [self makePrediction];

}

you can remove that line you have currently there since its taken care of in the makePrediction method

Jon Reese
Jon Reese
2,335 Points

That was it! Thank you so much for your help Stone Preston, I greatly appreciate it man, you're awesome!

Stone Preston
Stone Preston
42,016 Points

no problem glad we got it figured out.

Andrew Brotherton
Andrew Brotherton
7,515 Points

I've got all of that and mine isn't animating at all either. @implementation THViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.crystalBall = [[THCrystalBall alloc] init]; self.backgroundImageView.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"CB000001"], [UIImage imageNamed:@"CB000003"], [UIImage imageNamed:@"CB000004"], [UIImage imageNamed:@"CB000005"], [UIImage imageNamed:@"CB000006"], [UIImage imageNamed:@"CB000007"], [UIImage imageNamed:@"CB000008"], [UIImage imageNamed:@"CB000009"], [UIImage imageNamed:@"CB000010"], [UIImage imageNamed:@"CB000011"], [UIImage imageNamed:@"CB000012"], [UIImage imageNamed:@"CB000013"], [UIImage imageNamed:@"CB000014"], [UIImage imageNamed:@"CB000015"], [UIImage imageNamed:@"CB000016"], [UIImage imageNamed:@"CB000017"], [UIImage imageNamed:@"CB000018"], [UIImage imageNamed:@"CB000019"], [UIImage imageNamed:@"CB000020"], [UIImage imageNamed:@"CB000021"], [UIImage imageNamed:@"CB000022"], [UIImage imageNamed:@"CB000023"], [UIImage imageNamed:@"CB000024"], [UIImage imageNamed:@"CB000025"], [UIImage imageNamed:@"CB000026"], [UIImage imageNamed:@"CB000027"], [UIImage imageNamed:@"CB000028"], [UIImage imageNamed:@"CB000029"], [UIImage imageNamed:@"CB000030"], [UIImage imageNamed:@"CB000031"], [UIImage imageNamed:@"CB000032"], [UIImage imageNamed:@"CB000033"], [UIImage imageNamed:@"CB000034"], [UIImage imageNamed:@"CB000035"], [UIImage imageNamed:@"CB000036"], [UIImage imageNamed:@"CB000037"], [UIImage imageNamed:@"CB000038"], [UIImage imageNamed:@"CB000039"], [UIImage imageNamed:@"CB000040"], [UIImage imageNamed:@"CB000041"], [UIImage imageNamed:@"CB000042"], [UIImage imageNamed:@"CB000043"], [UIImage imageNamed:@"CB000044"], [UIImage imageNamed:@"CB000045"], [UIImage imageNamed:@"CB000046"], [UIImage imageNamed:@"CB000047"], [UIImage imageNamed:@"CB000048"], [UIImage imageNamed:@"CB000049"], [UIImage imageNamed:@"CB000050"], [UIImage imageNamed:@"CB000051"], [UIImage imageNamed:@"CB000052"], [UIImage imageNamed:@"CB000053"], [UIImage imageNamed:@"CB000054"], [UIImage imageNamed:@"CB000055"], [UIImage imageNamed:@"CB000056"], [UIImage imageNamed:@"CB000057"], [UIImage imageNamed:@"CB000058"], [UIImage imageNamed:@"CB000059"], [UIImage imageNamed:@"CB000060"], nil];

    self.backgroundImageView.animationDuration = 2.5f; self.backgroundImageView.animationRepeatCount = 1; }

pragma mark - Prediction

  • (void) makePrediction { [self.backgroundImageView startAnimating]; self.predictionLabel.text = [self.crystalBall randomPrediction]; }

pragma mark - Motion Events

  • (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { self.predictionLabel.text = nil; }

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if ( motion == UIEventSubtypeMotionShake) { [self makePrediction]; } }

  • (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"motion canceled"); }

pragma marks- Touch Events

  • (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { self.predictionLabel.text = nil; }

  • (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self makePrediction]; }

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touches canceled"); }

@end