【问题标题】:Is there a way to force the interface orientation to change in iPhone?有没有办法强制改变 iPhone 的界面方向?
【发布时间】:2011-06-14 17:06:01
【问题描述】:

我知道如何使用

来允许/禁止方向旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

但是,我现在遇到的问题是手机可能处于纵向(或倒置)状态,并且在某些时候我想旋转屏幕,就好像用户旋转到横向一样。在这一点上,我不希望自动旋转工作了。我想强制界面方向保持横向。有没有办法做到这一点?我可能会想出一个关闭自动旋转的技巧,但首先强制旋转我不知道该怎么做。

这是我想要做的:

  • 应用程序可以旋转到任何方向。一切正常。

  • 发生了一个事件。

  • 现在自动旋转仅适用于横向左和横向。此外,如果用户处于纵向或上下颠倒状态,我会以编程方式旋转到横向以将用户锁定到该方向。

只是为了让事情变得更棘手,我想在强制旋转之前弹出一个UIAlertView(我知道怎么做),让用户知道发生了什么,我想在 alertView 后面进行屏幕旋转但是不是警报视图。这甚至可能吗?

谢谢。

【问题讨论】:

    标签: ios uiinterfaceorientation


    【解决方案1】:

    适合您的情况的最佳解决方案是通知用户将其转为横向并暂停程序,直到用户转为方向。然后当用户旋转设备时,您可以处理委托方法并开始以横向模式显示屏幕。这种方法已在许多应用商店应用程序中得到处理,看起来它是让应用在应用商店获得批准的最佳方法之一。我希望这是有道理的。

    【讨论】:

      【解决方案2】:
      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
              return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
          }
      
      
          // orientation view swapping logic
      - (void)didRotate:(NSNotification *)notification {
      
      
          UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
          if (newOrientation != UIDeviceOrientationUnknown || newOrientation != UIDeviceOrientationFaceUp || newOrientation != UIDeviceOrientationFaceDown) 
          {
                 orientation = newOrientation;
      
          }
      
          // Write your orientation logic here
              if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) 
          {
           // Clear the current view and insert the orientation specific view.
               [self clearCurrentView];
               [self.view insertSubview:yourLandscapeView atIndex:0];
      
          } else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) 
          {
                 // Clear the current view and insert the orientation specific view.
                  [self clearCurrentView];
                  [self.view insertSubview:yourPortraitView atIndex:0];
      
           }
      }
      

      并在事件中添加该通知

      【讨论】:

        【解决方案3】:

        你不能。至少不是以苹果允许的方式。自转设计为在整个应用程序中是相同的。您可以编写手动旋转视图的代码,但就 UIDevice 而言,您将处于相同的方向。

        【讨论】:

          【解决方案4】:

          根据 Apple 的说法,如果您不希望设备方向旋转 API 提供的默认行为:

          您可以控制:

          • 您的应用支持的方向
          • 两个方向之间的旋转如何在屏幕上显示动画。

          我读这篇文章的方式是“你可以控制这些事情中的任何一个,但不能控制其他事情”或“你只能控制这两个事情。”

          我在文档中没有看到任何明确告诉您如何“强制”改变方向的内容。所有这些都由设备处理,因此对于持有设备的人来说似乎更自然和友好。这有意义吗?

          在此处查看有关轮换更改的文档:

          http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

          最后,我想说这是不可能的,而且苹果也不会批准。我对 Praveen 的回答投了赞成票,因为我认为您可能需要考虑一种不同的方法来解决您正在尝试做的事情。

          【讨论】:

            【解决方案5】:

            是的,你可以这样做。您要做的是在 -(void) viewwillappear 方法中简单地包含以下代码

            if (self.interfaceOrientation == <current orientation>) {
                    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)<to which orientation you want to change.>];
                }
            

            它会解决你的问题

            【讨论】:

            • 这个API是隐藏的,UIDevice实例的orientation属性是只读的,所以在Apple审核过程中可能会给您带来问题。
            • 对于,UIInterfaceOrientation 的类型不能强制为id。解决方法是什么?
            猜你喜欢
            • 1970-01-01
            • 2014-04-17
            • 1970-01-01
            • 1970-01-01
            • 2021-01-08
            • 2016-02-17
            • 2011-12-10
            • 2015-04-08
            • 1970-01-01
            相关资源
            最近更新 更多