如何检测改变ipad的方向

时间:2014-10-14 09:22:36

标签: ios ipad uiinterfaceorientation

我正在创建一个应该支持横向和纵向的iPad应用程序。我在AppDelegate中设置了根视图控制器。这个视图是我的Splash屏幕视图。

`

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
    //Do what you want in Landscape Left
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
    //Do what you want in Landscape Right
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
    //Do what you want in Portrait
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
    //Do what you want in Portrait Upside Down
    viewController1=[[SplashLandscapeViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
}





self.window.rootViewController = viewController1;
[self.window makeKeyAndVisible];

return YES;

}

这很好用。并且这个viewcontroller1在4,5秒后消失并加载另一个viewcontroller。我的问题是,当viewcontroller1显示在屏幕上时,如果用户转动ipad,我怎么能识别它并在删除当前的肖像xib后加载我的横向.xib。

请帮帮我。 谢谢

1 个答案:

答案 0 :(得分:0)

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];

add this method into the app view controller for getting notification.

-(void)changeOrientation {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
    }

}