【问题标题】:When an iOS device rotates, how do I change the orientation only one element?当 iOS 设备旋转时,如何仅更改一个元素的方向?
【发布时间】:2012-06-08 13:57:22
【问题描述】:

我希望能够在我当前的 iPhone 应用程序中处理方向变化。问题是我不希望我的控制器的视图旋转或调整大小。当设备方向改变时,我只想让一个 UI 元素在屏幕上旋转 90 度。

Apple 的相机应用程序就是一个很好的例子 - 当设备方向改变时,所有按钮都会旋转,但视图本身不会旋转。

我想我可以调整视图大小,移动所有元素,然后为按钮设置动画,但我觉得必须有更简单的方法。

提前致谢。

【问题讨论】:

    标签: iphone ios5


    【解决方案1】:

    1) 您可以创建 2 个视图层次结构并更改屏幕上的 willRotateToInterfaceOrientation: see this question

    2) 或者您可以自己处理所有旋转 see this question

    【讨论】:

    • 选项 2 非常完美。感谢您为我指明正确的方向。由于某种原因,我很难找到那个。感谢您的帖子和示例代码。太棒了!
    • @tnbeatty 我对引用的答案进行了编辑,因为它错误地处理了 UIDeviceOrientation 的一些值
    【解决方案2】:

    你可以使用

        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
    if(interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
        {
            //Change frames as per orientations
        }
    
        else
        {    
              //Change frames as per orientations
        }
    
    
        return NO;
        }
    

    并根据方向手动设置要更改的 UI 元素的框架。 您可以从this docConstants 部分访问所有方向。

    编辑: 你可以使用 NSNotification 来解决这个问题。

    UIApplicationDidChangeStatusBarOrientationNotification
    

    当应用程序的用户界面的方向发布 变化。

    userInfo 字典包含一个 NSNumber 对象,该对象封装 UIInterfaceOrientation 值(请参阅 UIInterfaceOrientation)。采用 UIApplicationStatusBarOrientationUserInfoKey 来访问这个值 可用性

    Available in iOS 2.0 and later.
    

    在 UIApplication.h 中声明

    【讨论】:

    • 这是一个好主意——这是我首先尝试的。问题是:假设设备以 UIInterfaceOrientationPortrait 启动。如果用户将设备转到 UIInterfaceOrientationLandscapeLeft,我可以检测到该更改,然后更改框架。如果用户随后旋转回 UIInterfaceOrientationPortrait,则不会调用此方法。
    • 这很有趣,根据我的经验,这个例程会在每次发生的旋转 (true) 时被调用。
    • 我应该澄清一下:在我之前的评论中, -shouldAutorotateToInterfaceOrientation 不会被调用,因为设备方向实际上从未改变。因此,手机认为它是纵向的,并且当设备变回纵向时不会调用此方法。这有意义吗?
    • 我完全同意你的看法。建议另一种方式,请检查编辑。
    • 不幸的是它没有。为什么实际的设备方向永远不会改变,但它会以某种方式变回纵向?那从什么开始?
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多