首页 \ 问答 \ iOS切换视图控制器取决于设备方向(iOS switching view controllers depending on the device orientation)

iOS切换视图控制器取决于设备方向(iOS switching view controllers depending on the device orientation)

我正在开发一个增强现实应用程序,一切正常,直到现在我需要两种不同的可视化(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文件。 我非常感谢任何建议! 谢谢


I'm developing an Augmented Reality application, everything worked properly till now that I need two different kind of visualization (AR and Map) depending on the device orientation. In particular the application should use the landscapeViewController when the device is in landscape mode while it should use another controller (named faceUpViewController ) when the device's orientation is "face up". I tried doing it with two simple view controllers and it works fine. The problem happens when the landscapeViewController uses the AR controller. The view is completely white and I don't understand why. Both the two controllers are "contained" by a Root View Controller. I'm doing everything by coding so without nib files. Here is the code:

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 Very simple version

- (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

I say again that I'm doing everything by coding thereby without nib files. I really appreciate any advice! Thanks


原文:https://stackoverflow.com/questions/8406465
更新时间:2022-08-25 18:08

最满意答案

一个安全的方法是使用mkstemp(3) 。


A safe way is to use mkstemp(3).

相关问答

更多
  • 至少如果您使用现有的Python库打开临时文件,则在Windows中无法从多个进程访问它。 根据MSDN,您可以指定第三个参数( dwSharedMode )共享模式标志FILE_SHARE_READ到CreateFile()函数,其中: 启用文件或设备上的后续打开操作以请求读取访问权限。 否则,如果其他进程请求读取访问权限,则无法打开文件或设备。 如果未指定此标志,但文件或设备已被打开以进行读取访问,则该功能失败。 所以,你可以编写一个Windows特定的C例程来创建一个自定义的临时文件打开器函数,从Py ...
  • 一个安全的方法是使用mkstemp(3) 。 A safe way is to use mkstemp(3).
  • mktemp(1)手册页面很好地解释了: 传统上,许多shell脚本使用pid作为后缀使用程序的名称,并将其用作临时文件名。 这种命名方案是可预测的,它创建的竞争条件容易让攻击者获胜。 更安全,但仍然较差的方法是使用相同的命名方案创建一个临时目录。 虽然这样做可以确保临时文件不会被颠覆,但它仍然允许简单的拒绝服务攻击。 由于这些原因,建议使用mktemp。 在脚本中,我调用mktemp的东西 mydir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename 0).XXXXXXXXX ...
  • 您可以从InputStream中放入任意文件: put(InputStream src, String dst) 。 您可以使用以下内容上传空文件: put( new ByteArrayInputStream( "".getBytes() ), "tempfile"); You can put an arbitrary file from an InputStream: put(InputStream src, String dst). You can upload an empty file with ...
  • 如果有人遇到这种情况,我从未想过在用户选择下载位置之前移动临时文件的HTML5文件系统方法。 相反,我使用带有电子showSaveDialog的nodejs文件系统。 我还必须更改JSZip以使用.generateNodeStream而不是.generateAsync。 以下是我为我工作的功能。 savePNGButton.addEventListener('click', function(e) { var zip = new JSZip(); if (WatermarkText == ""){ ...
  • Document.SaveAs的第五个参数是AddToRecentFiles 。 将其设置为False。 https://msdn.microsoft.com/en-us/library/office/aa220734 您可以创建临时文件,将它们合并为一个,然后关闭它们而不保存它们。 The fifth parameter of Document.SaveAs is AddToRecentFiles. Set that to False. https://msdn.microsoft.com/en-us/l ...
  • 我不知道Windows中的任何进程会自动删除临时文件。 用户可能会设置一个清理作业,但您可以合理地预期您的文件至少会被保留一两天。 相比之下,当您在Outlook中打开附件时,它会将附件复制到临时文件并启动关联的应用程序。 如果用户从不关闭关联的应用程序,那么这些临时附件文件可能需要无限期地停留。 I'm not aware of any process in Windows that deletes temporary files automatically. The user may have a cl ...
  • 这将创建一个临时目录: string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectory); This will create a temporary directory: string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRando ...
  • 有争议的文件称为锁定文件 - 从Emacs版本24.3开始,可以使用以下设置进行控制: (setq create-lockfiles nil) https://stackoverflow.com/a/12974060/2112489 The file at issue is called a lock file -- commencing with Emacs version 24.3, it can be controlled with the following setting: (setq crea ...
  • 使它工作。 变 upload_tmp_dir = "C:/Users/server/Pictures/tmp" to "C:\TEMP". 在C中创建文件夹TEMP并授予权限。 似乎只有在直接连接到C:时才有效。 Made it work. Changed upload_tmp_dir = "C:/Users/server/Pictures/tmp" to "C:\TEMP". Create the folder TEMP in C and gave the permissions. Seems lik ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)