How do I detect orientation on app launch for splash screen animation on iPad!
Asked Answered
W

9

18

Hi I have an app and I have two *.pngs for default splash screen:

Default-Landscape.png Default-Portrait.png

What I want is to animate this default splash screen away when my app is loaded and ready to go.

To achieve this I would normally present an UIImageView with either default-landscape or default-portrait (depending on the device orientation), keep it on screen for a certain time and then animate it away.

My problem is that if I call [[UIDevice currentDevice] orientation] in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

The answer is always that the device is in portrait orientation even if I clearly have it in landscape. I tried this in simulator and on the device as well and the behaviour is the same.

Does anyone know a fix for this or maybe some other approach?

Thanks!

Wallacewallach answered 13/8, 2010 at 14:5 Comment(1)
@Horatiu Paraschiv I am also having this problem. Can you please help me to find the solution?Karelian
M
5

I had troubles with this and I solved it by making one image 1024x1024 and setting the contentMode of the UIImageView to UIViewContentModeTop, then using left and right margin autoresizing. So long as your portrait and landscape default images are the same layout then this will work fine.

Just to clarify here's what I used:

bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SplashImage]];
bgView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
bgView.contentMode = UIViewContentModeTop;
Mercymerdith answered 8/8, 2011 at 10:8 Comment(1)
Great idea - I ended up doing this too.Viminal
L
3

To get around this problem I installed the splash image view inside of a view controller that allowed both orientations. Even though the device reported the wrong orientation at startup, the view controller seems to get the right one.

Layer answered 26/8, 2010 at 18:24 Comment(0)
R
3

You can use UIApplication statusBarOrientation as follows:

if ( UIDeviceOrientationIsLandscape( [[UIApplication sharedApplication] statusBarOrientation] ))
    {
    // landscape code
    }
else
    {
    // portrait code
    }
Rafaelita answered 20/11, 2012 at 7:0 Comment(0)
V
1

Maybe you could show a blank view with black background at start time and place [[UIDevice currentDevice] orientation] into this view's viewDidAppear and start your splash screen from there?

Vienna answered 13/8, 2010 at 14:38 Comment(1)
This would work and do what you wanted, but it goes against the guidelines in the iOS HIG -- the launch image isn't meant to be a splash screen, and iOS apps shouldn't use splash screens.Lareine
W
1

Another solution would be to read the accelerometer data and determine the orientation yourself.

Wildee answered 13/8, 2010 at 19:38 Comment(0)
M
1

To know at start what is the orientation (UIDevice orientation don't work until user have rotate the device) intercept shouldAutorotateToInterfaceOrientation of your View Controller, it is called at start, and you know your device orientation.

Marozik answered 13/1, 2011 at 0:36 Comment(0)
S
-1

There are certainly times when you want to transition from the loading image to something else before the user gets control of your app. Unless your app is really simple, going from loading image to landing page probably won't be sufficient without making the app experience really suck. If you ever develop a large app, you'll definitely want to do that to show progress during setup, loading xibs, etc. If an app takes several seconds to prepare with no feedback, users will hate it. IMO, there's nothing wrong with a nice transition effect either. Almost nobody uses loading screens the way Apple suggests to. I don't know which apps you looked at that showed the "empty UI" type loading screens they suggest. Heck, even Apple doesn't do that except in their sample code and none of my clients would find that acceptable. It's a lame design.

Schnell answered 12/11, 2010 at 1:7 Comment(1)
Eh.. that was in response to Alex.Schnell
S
-1

Only the first view added to the window is rotated by the OS. So if you want your splash screen to automatically rotate AND your main view is rotatable then just add it as a child of that view.

Here is my code for fading out the appropriate splash screen image:

    // Determine which launch image file
NSString * launchImageName = @"Default.png";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
        launchImageName = @"Default-Portrait.png";
    } else {
        launchImageName = @"Default-Landscape.png";
    }
}

// Create a fade out effect
UIImageView* whiteoutView = [[[UIImageView alloc] initWithFrame:self.window.frame] autorelease];
whiteoutView.autoresizingMask = UIViewAutoresizingFlexibleWidth  | UIViewAutoresizingFlexibleHeight;
whiteoutView.autoresizesSubviews = YES;         
whiteoutView.image = [UIImage imageNamed:launchImageName];
[[[self.window subviews] objectAtIndex:0] addSubview:whiteoutView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
whiteoutView.alpha = 0.0;
[UIView commitAnimations];

Note: You'll have to update it to support hi-res screens.

Satem answered 22/2, 2011 at 1:42 Comment(1)
Does not work, [UIDevice currentDevice] orientation] is "undefined" for splash screen use.Samons
L
-2

It sounds like you're not using the launch image the way Apple recommends in the iOS HIG. It specifically calls out that you should not use it as a splash screen, but rather as a skeleton version of your actual UI. The net effect is that your app appears to be ready just that much faster.

The suggestions that you could draw a splash screen yourself after the app has launching in viewDidAppear or similar also are missing the basic purpose of a launch image. It's not a splash screen. If your app is ready, let the user interact with it, don't waste their time drawing a splash screen.

From my five minute survey of Apple apps and third-party apps, everyone showed a portrait launch image, loaded the portrait version of the UI, and then rotated to landscape. It's been a while since programming on iOS, but I think this mirrors the order of the method calls -- first your app gets launched, then it is told to rotate to a particular orientation.

It might be a nice enhancement request to file with Apple though :)

Lareine answered 13/8, 2010 at 21:1 Comment(2)
Thank you for the documented response. I do have a different opinion though. Apple recommends that you should use a skeleton version of your actual UI to fool the user that the app is loading faster than it actually is. Going past this minor trick most apps use a splash screen with the company's logo or something attractive to get the user attention until the app is ready to go.Wallacewallach
What I want to do is make the transition between the splash screen and my UI smoother (add an effect) thus improving the user experience. The transition would be 0.3-0.5 seconds at most so I don't think the user would feel that this is a waste of their time.Wallacewallach

© 2022 - 2024 — McMap. All rights reserved.