iOS切换视图控制器取决于设备方向

时间:2011-12-06 20:35:06

标签: iphone ios uiview uiviewcontroller augmented-reality

我正在开发一个增强现实应用程序,一切正常,直到现在我需要两种不同的可视化(AR和Map),具体取决于设备方向。特别是当设备处于横向模式时应用程序应该使用landscapeViewController,而当设备的方向是“正面朝上”时应该使用另一个控制器(名为faceUpViewController)。我尝试使用两个简单的视图控制器,它工作正常。当landscapeViewController使用AR控制器时会发生此问题。视图完全是白色的,我不明白为什么。这两个控制器都由Root View Controller“包含”。我正在通过编码来做所有事情,所以没有nib文件。这是代码:

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}

- (void)deviceOrientationDidChange:(NSNotification *)notification{

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        if (self.landscapeViewController.view.superview == nil) {
            if (self.landscapeViewController == nil) {
                LandscapeViewController *lvc = [[LandscapeViewController alloc] init];
                self.landscapeViewController = lvc;
                [lvc release];
            }
            [self.faceUpViewController.view removeFromSuperview];
            [self.view addSubview:self.landscapeViewController.view];
        }
    }

    if (orientation == UIDeviceOrientationFaceUp) {
        if (self.faceUpViewController.view.superview == nil) {
            if (self.faceUpViewController == nil) {
                FaceUpViewController *fvc = [[FaceUpViewController alloc] init];
                self.faceUpViewController = fvc;
                [fvc release];
            }
            [self.landscapeViewController.view removeFromSuperview];
            [self.view addSubview:self.faceUpViewController.view];
        }
    }

}

@end

LandscapeViewController.m

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    UIView *landscapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    landscapeView.backgroundColor = [UIColor yellowColor];
    self.view = landscapeView;
    [landscapeView release];

    ARController *arC = [[ARController alloc] initWithViewController:self];
    arC.landscapeViewController = self;
    self.arController = arC;
    [arC release];
}

//When the view appear present the camera feed
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [_arController presentModalARControllerAnimated:NO];
}

FaceUpViewController.m

- (void)loadView
{
    UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    faceUpView.backgroundColor = [UIColor blueColor];
    self.view = faceUpView;
    [faceUpView release];
}

ARController.m 非常简单的版本

- (id) initWithViewController:(UIViewController *)theView{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        self.rootController = theView; 

        //Retrieve screen bounds
        CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

        UIView *overlaidView = [[UIView alloc] initWithFrame: screenBounds];
        self.overlayView =  overlaidView;
        [overlaidView release];
        self.rootController.view = overlayView;

        // Initialise the UIImagePickerController 
        UIImagePickerController *picker= [[UIImagePickerController alloc] init];
        self.pickerController = picker;
        [picker release];

        self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
        self.pickerController.cameraViewTransform = CGAffineTransformScale(
                                                                           self.pickerController.cameraViewTransform, 1.0f, 1.12412f);

        self.pickerController.showsCameraControls = NO; 
        self.pickerController.navigationBarHidden = YES; 
        self.pickerController.cameraOverlayView = _overlayView;
    }

    return self;
}

- (void)presentModalARControllerAnimated:(BOOL)animated{
    [self.rootController presentModalViewController:[self pickerController] animated:animated]; 
    self.overlayView.frame = self.pickerController.view.bounds;
}

@end

我再次说,我正在通过编码来完成所有事情而不使用nib文件。 我非常感谢任何建议! 感谢

2 个答案:

答案 0 :(得分:4)

在此处添加和删除“子”视图控制器视图的主要问题是视图控制器生命周期方法(viewWillAppear:viewDidAppear:等)赢了你的孩子视图控制器会被调用。像UINavigationControllerUITabBarController这样的容器一直都知道如何将这些方法适当地委托给他们的孩子,但UIViewController没有正式支持在您自己的自定义容器下嵌套视图控制器的能力iOS 5.这是可能的,但要做得更好还需要做更多的工作。

如果您想坚持添加和删除子视图的方法,您有两种选择:

  1. 需要iOS 5+,并致电addChildViewController:removeFromParentViewControllertransitionFromViewController:toViewController:duration:options:animations:completion:willMoveToParentViewController:,和 didMoveToParentViewController:类引用的Implementing a Container View Controller部分中描述的UIViewController

  2. 要支持较旧的iOS版本,您必须覆盖UIViewController类的许多方法,并将这些调用手动委派给子视图控制器,以使它们按预期运行。我会特别注意UIViewController班级参考中标题为“回应查看事件”和“回应查看轮换事件”的部分。

  3. iOS 5之前支持的另一种方法是使用presentModalViewController:animated:呈现您的子视图控制器,而不是将其视图作为子视图添加到容器中。 Apple在适用于iOS的View Controller编程指南的Creating an Alternate Landscape Interface部分中描述了这种方法。这种方法的优点是您的子视图控制器被正式支持为视图控制器层次结构的一等成员,因此UIKit将自动适当地管理其生命周期。您不必手动覆盖和委托所有这些方法。

答案 1 :(得分:1)

您可能希望尝试提高您的录取率 - 更多人愿意帮助您。

无论如何,猜测:在根控制器中,尝试放入

的内容
  deviceOrientationDidChange

  deviceOrientationWillChange.