I've got an iPhone app with a little animation at startup. The desired effect is a fade/zoom of the Default.png. How I accomplish this is adding an image view in the app delegate that has the same image, then I do a UIViewAnimation block on it. Works just fine.
Enter iPad, and the conversion process to make this a universal app.
I know about Default-Landscape.png and Default-Portrait.png and those work just fine. But since it could be either image, I need to know which one to put in my image view for the animation.
The problem I'm encountering is that everything I know to ask is reporting portrait, even if the device/simulator launches landscape. Checking the frame of the UIWindow gives portrait dimensions, as does [UIScreen mainScreen]. Asking UIDevice for orientation reports portrait. Since this is the app delegate, I don't have a view controller yet to ask for orientation.
I found a little nugget on the Apple dev forums that says that view controllers are always created portrait, and then rotated without animation if necessary before being put on the screen. So I change my approach and use a view controller instead of just the image view.
This always goes portrait:
Code:
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewWillAppear:animated];
if ( ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft) || ([self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) )
{
NSLog(@"Using landscape image");
[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Landscape.png"]];
}
else
{
NSLog(@"Using portrait image");
[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Portrait.png"]];
}
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
I got this to work, but it seems a tad unnecessary. And it only works on the device, it does not work in the simulator.
Code:
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[super viewWillAppear:animated];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
{
NSLog(@"Using landscape image");
[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Landscape.png"]];
}
else
{
NSLog(@"Using portrait image");
[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Portrait.png"]];
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
I feel like I'm missing something obvious, but nothing else I've tried is working. Surely there has to be something easier, and something that would work on the simulator too. Any tips?