Bummer! You have been redirected as the page you requested could not be found.

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

ios animation not working

I tried to animate my app but it wont work, ill copy paste the code below: viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
}


@property (strong, nonatomic) IBOutlet UILabel *predictionLabel;
@property (strong, nonatomic) NSArray *predictionArray;
@property (strong, nonatomic) UIImageView *imageView;

-(void) makePrediction;

@end

viewcontroller.m:


#import "ViewController.h"

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"background.png"];
    self.imageView = [[UIImageView alloc]initWithImage:image];
    [self.view insertSubview:self.imageView aboveSubview:0];

    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 to answer now",@"Please stop bothering me",@"It is true",@"It is false", @"Really stop bothering me", nil];
    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;

}

- (void)viewDidUnload
{
    [self setPredictionLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


-(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 (motion == 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

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

If you have an image view within your storyboard then it could be hiding the image view created within the viewDidLoad method. You should delete the one in your storyboard.

I tried that, then when I run my app all I get is a blank white screen

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

On the third line of your viewDidLoad method you should use the following method:

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

The method you are using will not work because you are passing 0 while it expects an instance of UIView .