【问题标题】:UITraitCollection iPad how to detect the current orientationUITraitCollection iPad如何检测当前方向
【发布时间】:2015-02-22 16:21:22
【问题描述】:

由于 iOS8 检测 UIViewController 方向的方法已被弃用。
例如 -interfaceOrientation 已被弃用,为了检测当前视图控制器方向,我们似乎需要向视图控制器上的 -traitCollection 属性询问此信息。
我有点困惑 iPad 在两个方向上都将他的特征设置为常规。
检测当前方向的最佳方法是什么?

【问题讨论】:

    标签: ios ipad ios8 uiinterfaceorientation orientation-changes


    【解决方案1】:

    使用 UIDevice 的-orientation 属性是不正确的(即使它在大多数情况下都可以工作)并且可能导致一些错误,例如UIDeviceOrientation 如果设备朝上或朝下,还要考虑设备的方向,UIInterfaceOrientation 枚举中没有这些值的对。
    此外,如果您将应用程序锁定在某个特定方向,UIDevice 会在不考虑这一点的情况下为您提供设备方向。
    另一方面,iOS8 已弃用 interfaceOrientation 类上的 interfaceOrientation 属性。
    有 2 个选项可用于检测界面方向:

    • 使用状态栏方向
    • 在 iPhone 上使用尺寸类,如果它们没有被覆盖,它们可以让您了解当前界面方向

    仍然缺少一种了解界面方向变化方向的方法,这在动画期间非常重要。
    在 WWDC 2014 “iOS8 中的视图控制器改进”的会议上,演讲者提供了一种解决方案那个问题也是,使用替换-will/DidRotateToInterfaceOrientation的方法。

    这里建议的解决方案部分实施:

    -(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
        orientation = [self orientationFromTransform: [t targetTransform]]; 
        oldOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
        [self myWillRotateToInterfaceOrientation:orientation duration: duration]; 
        [t animateAlongsideTransition:^(id <UIVCTCContext>) {
             [self myWillAnimateRotationToInterfaceOrientation:orientation
                                                      duration:duration];
          }
          completion: ^(id <UIVCTCContext>) {
             [self myDidAnimateFromInterfaceOrientation:oldOrientation];
          }];
    }
    

    【讨论】:

      【解决方案2】:

      查找 UIDevice 的 UIDeviceOrientation 部分及其方向属性。文档是here。这就是我进行方向检查的方式。您还可以查看屏幕宽度是否大于屏幕高度,因为有时,特别是在第一次启动应用程序时,它会是 UIDeviceOrientationUnknown。

      【讨论】:

      • 我不认为 UIDeviceOrientation 应该被用作 UIInterfaceOrientation 的替代品。事实是,如果你把你的设备放在一边,你会得到这个方向,而不是界面方向。当您需要设备的 3d 表示而不是 2d 界面时,UIDeviceOrientation 可以很好地工作。
      • 我一定误解了你想要完成的事情。
      【解决方案3】:

      最佳做法是使用尺寸等级而不是设备方向。

      iPad 在两个方向上都具有常规尺寸,因为无论方向如何,iPad 在垂直和水平方向都有足够的空间容纳内容。

      您应该考虑为什么您希望在纵向和横向方向上以不同的方式对待 iPad 显示器,并在此处更清楚地解释为什么尺寸等级不能满足您的需求。

      【讨论】:

      • 有时只是因为客户想要它。此外,在 wwdc2014 会议中,他们还解释了如何实现这一目标
      【解决方案4】:

      从 iOS 9 和新的多任务功能开始,Apple 建议使用 TraitCollection 或比较视图边界以适应您的设计需求。

      看看我对类似问题的回答: apple recommends you to compare view bounds

      【讨论】:

        猜你喜欢
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多