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

发布于 2024-12-16 18:38:11 字数 310 浏览 0 评论 0原文

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

[application statusBarOrientation];

但如果应用程序从关闭状态启动(即不从后台恢复),则它始终返回纵向,当从后台恢复时它可以工作。

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

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

谢谢!

I need to get interface orientation in appDidBecomeActive:(UIApplication *)application

[application statusBarOrientation];

but if the app starts from closed(ie not resumed from background), this always returns portrait, it works when resumed from background.

Also, I tried to use UIDevice orientation along with status bar orientation, but UIDevice orientation may not be the interface orientation.

So is there any way to get interface orientation in app delegate, appDidBecomeActive?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2024-12-23 18:38:11

您需要做的是在启动视图控制器中处理这个问题。您可以使用组合interfaceOrientation、shouldAutorotateToInterfaceOrientation、didAutorotateToInterfaceOrientation 等。

本质上,创建一个将作为根视图控制器的视图控制器。在那里,确定 shouldAutorotateToInterfaceOrientation 中的方向变化(在 viewDidLoad 中它始终是纵向或横向,具体取决于您的 xib,所以不要在那里执行此操作)。用 NSTimer 或其他什么东西来显示你的图像。计时器结束后,显示您的常规应用程序屏幕。

无论如何,除非有视图控制器,否则您无法显示图像,因此您必须等到视图控制器为您提供界面方向更改。您应该关注第一个视图控制器,而不是应用程序委托。

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];
}

What you need to do is handle this in your splash view controller. You can use a combination interfaceOrientation, shouldAutorotateToInterfaceOrientation, didAutorotateToInterfaceOrientation, etc.

Essentially, create a view controller that you will have as your root view controller. In there, determine the orientation changes in shouldAutorotateToInterfaceOrientation (it will always be portrait or landscape in viewDidLoad depending on your xib, so don't do it there). Do your image display with an NSTimer or whatever. After the timer, show your regular app screens.

You can't display the image until you have a view controller anyway, so you must wait until a view controller gives you the interfaceOrientation changes. You should focus on that first view controller, not the app delegate.

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文