Iphone iOS根据设备方向切换视图控制器
我正在开发一个增强现实应用程序,直到现在一切都正常工作,我需要两种不同类型的可视化(AR和Map),这取决于设备的方向。特别是,当设备处于横向模式时,应用程序应使用landscapeViewController,而当设备的方向为“面朝上”时,应用程序应使用另一个控制器(名为faceUpViewController)。我试着用两个简单的视图控制器来做,效果很好。当landscapeViewController使用AR控制器时,会发生此问题。景色完全是白色的,我不明白为什么。这两个控制器都由根视图控制器“包含”。我做的每件事都是通过编码,所以没有nib文件。代码如下: RootViewController.mIphone iOS根据设备方向切换视图控制器,iphone,ios,uiview,uiviewcontroller,augmented-reality,Iphone,Ios,Uiview,Uiviewcontroller,Augmented Reality,我正在开发一个增强现实应用程序,直到现在一切都正常工作,我需要两种不同类型的可视化(AR和Map),这取决于设备的方向。特别是,当设备处于横向模式时,应用程序应使用landscapeViewController,而当设备的方向为“面朝上”时,应用程序应使用另一个控制器(名为faceUpViewController)。我试着用两个简单的视图控制器来做,效果很好。当landscapeViewController使用AR控制器时,会发生此问题。景色完全是白色的,我不明白为什么。这两个控制器都由根视图控
- (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
// 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];
}
- (void)loadView
{
UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
faceUpView.backgroundColor = [UIColor blueColor];
self.view = faceUpView;
[faceUpView release];
}
LandscapeViewController.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
// 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];
}
- (void)loadView
{
UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
faceUpView.backgroundColor = [UIColor blueColor];
self.view = faceUpView;
[faceUpView release];
}
FaceUpViewController.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
// 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];
}
- (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文件的情况下进行编码。
我真的很感激任何建议!
谢谢你可能想试着提高一下你的录取率——更多的人会愿意帮助你 无论如何,胡乱猜测:在根控制器中,尝试将
deviceOrientationDidChange
进入
你可能想试着提高你的接受率——更多的人会愿意帮助你 无论如何,胡乱猜测:在根控制器中,尝试将
deviceOrientationDidChange
进入
在这里添加和删除“子”视图控制器视图的主要问题是,视图控制器生命周期方法(
视图将出现:
,视图显示:
,等等)永远不会在子视图控制器上调用。像UINavigationController
和uitabarcontroller
这样的容器一直都知道如何将这样的方法适当地委托给它们的子容器,但是在iOS 5之前,UIViewController
没有正式支持将视图控制器嵌套在您自己的自定义容器下的功能。这是可能的,但要做得正确需要做更多的工作
如果要坚持添加和删除子视图的方法,有两个选项:
addChildViewController:
,从父视图控制器中移除,
从ViewController:到ViewController:持续时间:选项:动画:完成:
,
将移动到ArentViewController:
,以及
didMoveToParentViewController:
如UIViewController
类参考部分所述
UIViewController
类的许多方法,并手动将这些调用委托给您的子视图控制器,以使它们按预期运行。我会特别注意UIViewController
类参考中标题为“响应视图事件”和“响应视图旋转事件”的部分支持iOS 5之前版本的另一种方法是使用
presentModalViewController:animated:
显示子视图控制器,而不是将其视图作为子视图添加到容器中。Apple在iOS的View Controller编程指南的一节中介绍了这种方法。这种方法的优点是,正式支持您的子视图控制器作为视图控制器层次结构的一流成员,因此UIKit将自动适当地管理它们的生命周期。您不必手动重写和委派所有这些方法。在这里添加和删除“子”视图控制器视图的主要问题是,视图控制器生命周期方法(视图将出现:
,视图显示:
,等等)永远不会在子视图控制器上被调用。像UINavigationController
和uitabarcontroller
这样的容器一直都知道如何将这样的方法适当地委托给它们的子容器,但是在iOS 5之前,UIViewController
没有正式支持将视图控制器嵌套在您自己的自定义容器下的功能。这是可能的,但要做得正确需要做更多的工作
如果要坚持添加和删除子视图的方法,有两个选项:
addChildViewController:
,从父视图控制器中移除,
从ViewController:到ViewController:持续时间:选项:动画:完成:
,
将移动到ArentViewController:
,以及
didMoveToParentViewController:
如UIViewController
类参考部分所述
UIViewController
类的许多方法,并手动将这些调用委托给您的子视图控制器,以使它们按预期运行。我会特别注意UIViewController
类参考中标题为“响应视图事件”和“响应视图旋转事件”的部分支持iOS 5之前版本的另一种方法是使用
presentModalViewController:animated:
显示子视图控制器,而不是将其视图作为子视图添加到容器中。Apple在iOS的View Controller编程指南的一节中介绍了这种方法。这种方法的优点是,正式支持您的子视图控制器作为视图控制器层次结构的一流成员,因此UIKit将自动适当地管理它们的生命周期。您不必手动重写和委托所有这些方法。不管怎样,您使用deviceOrientationWillChange的意思是什么?deviceOrientationDidChange是自定义方法