我正在开发一个增强现实应用程序,到目前为止一切正常,我需要两种不同类型的可视化(AR 和 map ),具体取决于设备方向。特别是当设备处于横向模式时,应用程序应该使用 LandscapeViewController,而当设备的方向是“面朝上”时,它应该使用另一个 Controller (名为 faceUpViewController )。我试过用两个简单的 View Controller 来做,效果很好。当 LandscapeViewController 使用 AR Controller 时,就会出现问题。 View 完全是白色的,我不明白为什么。这两个 Controller 都被 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 文件。
我真的很感激任何建议!
谢谢
最佳答案
像您在此处所做的那样添加和删除“子” View Controller 的 View 的主要问题是 View Controller 生命周期方法( viewWillAppear:
, viewDidAppear:
等)永远不会被您的 child 调用 View Controller 。像 UINavigationController
这样的容器和 UITabBarController
一直都知道如何将这些方法适本地委派给他们的 child ,但是 UIViewController
在 iOS 5 之前,没有正式支持将 View Controller 嵌套在您自己的自定义容器下的能力。这是可能的,但要做到这一点需要做更多的工作。
如果您想坚持添加和删除 subview 的方法,您有两种选择:
addChildViewController:
, removeFromParentViewController
,transitionFromViewController:toViewController:duration:options:animations:completion:
,willMoveToParentViewController:
, 和didMoveToParentViewController:
如 Implementing a Container View Controller 中所述UIViewController
的部分类引用。 UIViewController
的许多方法。类并将这些调用手动委托(delegate)给您的 subview Controller ,以使它们按预期运行。我会特别注意 UIViewController
中标题为“响应 View 事件”和“响应 View 轮换事件”的部分。类引用。 支持 iOS 5 之前的另一种方法是使用
presentModalViewController:animated:
呈现您的 subview Controller 。而不是将它们的 View 作为 subview 添加到容器中。 Apple 在 View Controller Programming Guide for iOS 的 Creating an Alternate Landscape Interface 部分中描述了这种方法。 .这种方法的优点是您的 subview Controller 被官方支持为 View Controller 层次结构的一等成员,因此 UIKit 将自动适本地管理它们的生命周期。您不必手动覆盖和委托(delegate)所有这些方法。
关于iphone - iOS 根据设备方向切换 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8406465/