0

我需要在 appDidBecomeActive:(UIApplication *)application 中获取界面方向

[application statusBarOrientation];

但是如果应用程序从关闭状态开始(即没有从后台恢复),这总是返回纵向,当从后台恢复时它可以工作。

另外,我尝试将 UIDevice 方向与状态栏方向一起使用,但 UIDevice 方向可能不是界面方向。

那么有什么方法可以在应用程序委托 appDidBecomeActive 中获取界面方向?

谢谢!

4

1 回答 1

1

您需要做的是在您的启动视图控制器中处理此问题。您可以使用组合 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];
}
于 2011-11-16T17:03:27.797 回答