代码之家  ›  专栏  ›  技术社区  ›  pgb

带多个笔尖的仅横向iPhone应用程序

  •  8
  • pgb  · 技术社区  · 15 年前

    我正在开发一个iPhone应用程序,它有几个笔尖,应该只是横向的。

    应用程序通过info.plist文件设置为以横向模式启动。

    我有两个视图控制器: FirstViewController SecondViewController .

    对于其中的每一个,我都有一个NIB文件,其中的视图是横向的。两个视图控制器都添加到我的 MainView NIB作为出口,它们的视图被懒惰地初始化。

    当应用程序加载时,第一个视图按预期以横向显示。但是,当我切换到第二个视图时,设备(或模拟器)仍处于横向,但视图会旋转,就像设备处于纵向模式一样,这会使我的界面刹车。

    在两者 UIViewController 类我有以下代码:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
    }
    

    要切换视图,在我的应用程序代理中,我正在执行以下操作:

    [viewController.view removeFromSuperview];
    [window addSubview:secondViewController.view];
    

    在哪里? viewController secondViewController 视图控制器连接的两个插座。

    这是第二个视图在ib中的外观: alt text http://img27.imageshack.us/img27/4898/picture1ni.png

    这就是它在模拟器中的样子: alt text http://img402.imageshack.us/img402/4866/picture2wt.png

    为什么第二个视图在横向显示,但界面旋转?

    我不想处理转换属性,因为这似乎太过分了。

    2 回复  |  直到 15 年前
        1
  •  3
  •   pix0r    15 年前

    我提出这个问题希望有人能给你一个有见地的回答,我会学到一些东西。遗憾的是,我担心您可能需要使用转换才能使其正常工作。下面是我最近用来解决问题的代码:

    - (void)forceLandscapeForView:(UIView *)theView {
      theView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
      theView.bounds = CGRectMake(0, 0, 480, 320);
      theView.center = CGPointMake(160, 240);
      [theView setNeedsLayout];
      [theView setNeedsDisplay];
    }
    

    然后,在添加新视图时,检查当前方向,如有必要,强制旋转:

    if (!UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
      [self forceLandscapeForView:_activeViewController.view];
    }
    

    当然,你会想对 shouldAutorotateToInterfaceOrientation 在每个视图控制器中:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    

    如果不是所有必要的话,我很想听听其他的解决方案。这个设置还有一个注意事项:如果您在视图之间进行了转换,并且在转换期间旋转手机,则视图方向可能会在错误的横向方向上翻转或“卡住”,这样您在浏览时需要翻转手机(横向右对横向左)意见。

        2
  •  0
  •   Morion    15 年前

    这只是一个建议,但是您可以尝试在第二个视图的shouldaotrotate方法中返回no。或者尝试在ib中的纵向视图中进行。您的视图似乎已正确加载(在横向模式下),但随后收到shouldAutoRotate消息并已旋转90度。