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

Crystal Ball App - Image Based Animation - Objective - C

I am trying to figure out why the animation isn't working on my crystal ball app. Here is my code. I have looked it over 5-6 times and can't figure out where I am going wrong.

Can someone help!

@implementation ViewController;
@synthesize predictionLabel;
@synthesize predictionArray;
@synthesize imageView;

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

    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:@"cball000010"],
                                      [UIImage imageNamed:@"cball000011"],
                                      [UIImage imageNamed:@"cball000012"],
                                      [UIImage imageNamed:@"cball000013"],
                                      [UIImage imageNamed:@"cball000014"],
                                      [UIImage imageNamed:@"cball000015"],
                                      [UIImage imageNamed:@"cball000016"],
                                      [UIImage imageNamed:@"cball000017"],
                                      [UIImage imageNamed:@"cball000018"],
                                      [UIImage imageNamed:@"cball000019"],
                                      [UIImage imageNamed:@"cball000020"],
                                      [UIImage imageNamed:@"cball000021"],
                                      [UIImage imageNamed:@"cball000022"],
                                      [UIImage imageNamed:@"cball000023"],
                                      [UIImage imageNamed:@"cball000024"],
                                       nil];

    self.imageView.animationDuration = 1.0;
    self.imageView.animationRepeatCount = 1;



    self.predictionArray = [[NSArray alloc] initWithObjects:
                                @"It is certain",
                                @"It is Decidedly 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 answer now", nil];
}

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

- (void) makePrediction{
    NSUInteger index = arc4random_uniform(self.predictionArray.count);

    self.predictionLabel.text = [self.predictionArray objectAtIndex:index];

    [self.imageView startAnimating];
}



- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    self.predictionLabel.text = @"";
}

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

    }
}

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

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

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


@end

5 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Zack have you also added an image view to your Storyboard? If so, chances are that it is hiding the image view you are creating programmatically. Delete the one from the Storyboard and you should see your animation work.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Zack within the motionEnded method the if statement needs to compare the argument motion with UIEventSubtypeMotionShake.

if (motion == UIEventSubtypeMotionShake) {
        [self makePrediction];
}

Basically you are checking to see if the motion event is a shake then you can make a prediction.

It look like that was the issue...thank you...I can now see my animation but the animation cuts off half way through...It looks like it's only cycling through the first 10 or so photos. Why do you think that is?

oh never mind I had too many zeros on once I hit double digits...thank you

One more question:

The animation only works every other time with the shake gesture. It works fine when it's pressed but not when I do the shake gesture...Do you see any flaws in the code that is causing that?

@interface ViewController ()


@end

@implementation ViewController;
@synthesize predictionLabel;
@synthesize predictionArray;
@synthesize imageView;

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

    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 Decidedly 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 answer now", nil];
}

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

- (void) makePrediction{
    NSUInteger index = arc4random_uniform(self.predictionArray.count);

    self.predictionLabel.text = [self.predictionArray objectAtIndex:index];

    [self.imageView startAnimating];


}



- (BOOL)canBecomeFirstResponder{
    return YES;
}

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

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

    }
}

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

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

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

}




@end