0

Alright, I'm almost positive I'm just doing something stupid, but I'm new to this, so please excuse my incompetence. I have a nested animation block, and it changes the view controller once it's finished - one problem. It fires prematurely. I don't know if it's me incorrectly changing the view controller, or the block is incorrect. Any help would be great. Thanks in advance.

SplashViewController.m

#import "SplashViewController.h"
#import "MenuViewController.h"

@interface SplashViewController ()

@end

@implementation SplashViewController

-(void) drawColoredBars
{
    UIView *greenBar = [[UIView alloc] initWithFrame:CGRectMake(75.0, -self.view.bounds.size.height, 3.0, self.view.bounds.size.height)];
    UIView *orangeBar = [[UIView alloc] initWithFrame:CGRectMake(87.5, -self.view.bounds.size.height - 25, 3.0, self.view.bounds.size.height)];
    UIView *yellowBar = [[UIView alloc] initWithFrame:CGRectMake(100.0, -self.view.bounds.size.height - 50, 3.0, self.view.bounds.size.height)];

    UIColor *green = [UIColor colorWithRed:142.0/255.0f green:149.0/255.0f blue:31.0/255.0f alpha:0.6f];
    UIColor *orange = [UIColor colorWithRed:204.0/255.0f green:82.0/255.0f blue:29.0/255.0f alpha:0.6f];
    UIColor *yellow = [UIColor colorWithRed:254.0/255.0f green:190.0/255.0f blue:16.0/255.0f alpha:0.6f];

    greenBar.backgroundColor = green;
    orangeBar.backgroundColor = orange;
    yellowBar.backgroundColor = yellow;

    greenBar.tag = 1;
    orangeBar.tag = 2;
    yellowBar.tag = 3;

    [self.view addSubview:greenBar];
    [self.view addSubview:orangeBar];
    [self.view addSubview:yellowBar];
}

-(void) animate
{
    MenuViewController *menu = [[MenuViewController alloc] init];
    double delay = 0.0;
    double x = 75.0;

    for (int i = 1; i < 4; i++) {

        CGRect frame = CGRectMake(x, 0.0, 3.0, self.view.bounds.size.height);

        [UIView animateWithDuration: 2.5
                              delay: delay
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             [self.view viewWithTag:i].frame = frame;
                         }
                         completion:^(BOOL finished){
                             [UIView animateWithDuration: 2.5
                                                   delay: 2.5
                                                 options: UIViewAnimationOptionCurveEaseIn
                                              animations:^{
                                                  self.view.alpha = 0.0;
                                              }
                                              completion:^(BOOL finished){
                                                  [self presentViewController:menu animated:NO completion:nil];
                                              }];
                         }];
        delay = delay + 0.5;
        x = x + 12.5;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    CAGradientLayer *layer = [GradientBackground drawGradient];
    layer.frame = self.view.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];

    [self drawColoredBars];
    [self animate];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

Once again, thanks!

jak1200
  • 45
  • 6

1 Answers1

0

The viewDidLoad method is too early to set up animations. Try putting [self animate] in viewDidAppear: instead.

Also, make sure [self.view viewWithTag:i] doesn't return nil. If it returns nil, the outer animation block doesn't have anything to animate and will fire its completion block immediately.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • It probably seems like a insane question, but where would I put it then? I'm very new to this. EDIT: I'm so sorry! I didn't read that right at all, my bad. Thanks. – jak1200 Sep 10 '14 at 03:27