如何在appDidBecomeActive:(UIApplication *)app中获得正确的界面方向?

时间:2011-11-16 05:58:11

标签: iphone delegates rotation orientation uidevice

我需要在appDidBecomeActive:(UIApplication *)应用程序中获取界面方向

[application statusBarOrientation];

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

此外,我尝试使用UIDevice方向以及状态栏方向,但UIDevice方向可能不是界面方向。

那么有没有办法在app delegate appDidBecomeActive中获得界面定位?

谢谢!

1 个答案:

答案 0 :(得分: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];
}