【问题标题】:iPhone SDK: Orientation (Landscape and Portrait views)iPhone SDK:方向(横向和纵向视图)
【发布时间】:2009-01-10 15:53:13
【问题描述】:

好的,我的普通应用程序处于纵向模式。我可以强制我的应用进入横向模式以获取如下视图(使用 navigationcontroller 和 viewcontroller):

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}

但是当我回到主菜单(tableview)时,它会直接回到纵向。我试试这段代码:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

但这不起作用..

有什么想法吗?

【问题讨论】:

    标签: cocoa-touch


    【解决方案1】:

    看一下 UIViewController 类中的函数shouldAutorotateToInterfaceOrientation:。如果您的 UIView 支持方向,则此函数返回 YES。如果您只对横向返回 YES,iPhone 将自动置于该方向。

    下面的代码应该可以做到。将它放在 UIViewController 中,该控制器控制您要置于横向模式的视图。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationLandscape);
    }
    

    【讨论】:

      【解决方案2】:

      为了让它工作,我将它添加到 rootviewcontroller:

      - (void)viewWillAppear:(BOOL)animated {
          [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
      
      }
      

      这似乎现在可以工作了。

      【讨论】:

      • 这算不算使用私有API?方向被记录为只读属性。
      • 这一行只能旋转模拟器。从逻辑上讲,你不能使用编程脚本让用户旋转他们的 iPhone;你只能给用户提示。
      • 其实 Shivan Raptor 是错的。只要您将 shouldAutoRotateTOInterfaceOrientation 设置为仅接受您强制设备更改为的方向,它将保持该方向(即使在设备上)。我建议在 ViewWillDisappear 中撤消此方向更改。
      • @Shivan 您是否希望手机从用户手中跳出并旋转到不同的方向?
      【解决方案3】:

      这是我正在做的事情:

      首先,将此定义放在文件顶部,就在您的#imports 下方:

      #define degreesToRadian(x) (M_PI * (x) / 180.0)
      

      然后,在 viewWillAppear: 方法中

      [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
      if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
          self.view.transform = CGAffineTransformIdentity;
          self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
          self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
      }
      

      如果你想让它被动画化,那么你可以将整个东西包装在一个动画块中,如下所示:

      [UIView beginAnimations:@"View Flip" context:nil];
      [UIView setAnimationDuration:1.25];
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
      
      [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
      if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
          self.view.transform = CGAffineTransformIdentity;
          self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
          self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
      }
      [UIView commitAnimations];
      

      然后,在您的纵向模式控制器中,您可以执行相反的操作 - 检查其当前是否为横向,如果是,请将其旋转回纵向。

      【讨论】:

      • 你很接近。 self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f); self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90)); self.view.center = CGPointMake(160.0f, 240.0f);我们必须将视图中心设置为屏幕中心,并填满屏幕(没有 20px 的空间)。
      • 如果不想隐藏状态栏可以添加[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
      【解决方案4】:

      您需要编辑 Info.plist 文件以添加具有适当值(UIInterfaceOrientationLandscapeRight 或 UIInterfaceOrientationLandscapeLeft)的 UIInterfaceOrientation 键。

      【讨论】:

      • 这只是回到纵向桌面主屏幕吗?因为我想要所有的肖像,只有一个风景。我可以让它变成横向,但是当我回去时,我必须旋转手机才能让它恢复到纵向。我不希望用户拥有其余的景观。
      【解决方案5】:

      您不能使用此私有 API。

      有一个解决方案:使用视图控制器并将其视图添加到窗口中。然后在该控制器中,您在 shouldAutorotate... 方法中强制景观。它工作正常,但请确保您的项目有必要使用它,因为强迫用户转动他的 iPhone 并不是很聪明。顺便说一下,如果您需要,这里有一个示例代码。

      http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html

      【讨论】:

        猜你喜欢
        • 2012-08-17
        • 2010-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多