Last Updated: February 25, 2016
·
3.686K
· _jeffreyjackson

Fade Out Splashscreen

Looking for an easy way to transition from your splashscreen to your rootViewController? Here is something you can pretty much copy and paste.

Since we'll be using some GCD for animation, be sure to declare a new queue in your app delegate.

dispatch_queue_t backgroundQueue;

What we're doing here is creating a new imageView with your splashscreen image file. When the splashscreen ends it'll open right up to your new imageView since it's front most subview of your rootViewController. You won't even see it switch.

What we're actually fading out is this new imageView not your splashscreen. The majority of the code here is just picking the correct splashscreen to fade out.

Place this somewhere in between self.window.rootViewController = ... and [self.window makeKeyAndVisible] in your didFinishLaunchingWithOptions method.

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.frame];

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    switch ( [[UIApplication sharedApplication] statusBarOrientation] )
    {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            if ( [UIScreen mainScreen].scale == 2 ) { imageView.image = [UIImage imageNamed:@"Default-Landscape@2x~ipad.png"]; }
            else { imageView.image = [UIImage imageNamed:@"Default-Landscape~ipad.png"]; }
            [imageView setFrame: CGRectMake(0, 0, 1024, 768) ];
            break;
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            if ( [UIScreen mainScreen].scale == 2 ) { imageView.image = [UIImage imageNamed:@"Default-Portrait@2x~ipad.png"]; }
            else { imageView.image = [UIImage imageNamed:@"Default-Portrait~ipad.png"]; }
            [imageView setFrame: CGRectMake(0, 0, 768, 1024) ];
            break;
    }
}
else
{
    if ( [UIScreen mainScreen].scale == 2 )
    {
        if ([UIScreen mainScreen].bounds.size.height == 568.0f)
        {
            imageView.image = [UIImage imageNamed:@"Default-568h@2x.png"];
            [imageView setFrame: CGRectMake(0, 0, 320, 568) ];
        }
        else
        {
            imageView.image = [UIImage imageNamed:@"Default@2x.png"];
            [imageView setFrame: CGRectMake(0, 0, 320, 480) ];
        }
    }
    else
    {
        imageView.image = [UIImage imageNamed:@"Default.png"];
        [imageView setFrame: CGRectMake(0, 0, 320, 480) ];
    }
}
[self.window.rootViewController.view addSubview:imageView];
[self.window.rootViewController.view bringSubviewToFront:imageView];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,2*100000),backgroundQueue, ^{
    [UIView transitionWithView:self.window
                      duration:1.00f
                       options:UIViewAnimationOptionCurveEaseOut
                    animations:^(void){
                        imageView.alpha = 0.0f;
                    }
                    completion:^(BOOL finished){
                        [imageView removeFromSuperview];
                    }];
});