您需要做的是在您的启动视图控制器中处理此问题。您可以使用组合 interfaceOrientation、shouldAutorotateToInterfaceOrientation、didAutorotateToInterfaceOrientation 等。
本质上,创建一个视图控制器作为根视图控制器。在那里,确定 shouldAutorotateToInterfaceOrientation 中的方向变化(在 viewDidLoad 中它总是纵向或横向,具体取决于您的 xib,所以不要在那里这样做)。使用 NSTimer 或其他方式显示图像。在计时器之后,显示您的常规应用程序屏幕。
无论如何,在您拥有视图控制器之前,您无法显示图像,因此您必须等到视图控制器为您提供 interfaceOrientation 更改。您应该专注于第一个视图控制器,而不是应用程序委托。
AppDelegate.h
#import <UIKit/UIKit.h>
@class SplashViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) IBOutlet UIWindow *window;
@property (retain, nonatomic) SplashViewController *splashController;
-(void)showSplash;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "SplashViewController.h"
@implementation AppDelegate
@synthesize window = _window, splashController = _splashController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self showSplash];
[self.window makeKeyAndVisible];
[self performSelector:@selector(registerBackground) withObject:nil afterDelay:5.0];
return YES;
}
-(void)showSplash
{
SplashViewController *splash = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
[self.window addSubview:splash.view];
self.splashController = splash;
[splash release];
//have to add a delay, otherwise it will be called on initial launch.
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(removeSplash:) userInfo:nil repeats:NO];
}
-(void)registerBackground
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(returnFromBackground:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
-(void)returnFromBackground:(NSNotification *)notification
{
[self showSplash];
}
-(void)removeSplash:(NSTimer *)timer
{
[self.splashController.view removeFromSuperview];
self.splashController = nil;
}
- (void)dealloc
{
[_window release];
[_splashController release];
[super dealloc];
}