当设备正面朝上时,检测主页按钮在哪一侧。

3
作为我目前正在开发的应用程序的一部分,我需要点击图像,然后显示正确方向的结果图像。请注意,用户可以在任何可用方向(纵向、横向左/右、面朝上)中单击图像。我能够实现所有模式的期望结果,但面朝上方向不行。
我的问题是:是否有一种方法检测设备面朝上时我的Home按钮在哪侧?如果有,我该如何做到这一点?请注意,该应用程序只适用于纵向应用程序。因此,我猜使用状态栏方向就不太可能了。
期待您对此的想法。

那些正在经历这个问题的人,我无法找到解决方法。这促使我转向计划B,即显示旋转按钮。 - iOSer
你找到这个问题的答案了吗?在这种情况下,我只能通过 UIScreen.main.bounds 的比率检测到面向上/未知时的纵横比,即 UIDeviceOrientation。 - Michał Ziobro
那个,这是我在以前公司的一个项目。我已经离开了,但据我所记得,这个问题仍然存在。 - iOSer
3个回答

2

你可以做:

if UIDevice.current.orientation == .portraitUpsideDown {
     print ("Divice oriented vertically, home button on top")
}

还有faceDownfaceUp等方向。请查看文档了解更多信息。


谢谢你的回答。获取主页按钮位置在纵向、倒置纵向、左横向和右横向模式下更容易。然而,当方向朝上时,这种方法将无法提供帮助。 - iOSer

0
基于您的需求,应该使用 UIInterfaceOrientation 而不是 UIDeviceOrientation,尽管它们有些相似,但具有不同的效果。
当您的设备方向为面朝上/下时,您的 UI 方向仍然是顶部、底部、左侧和右侧之一。
示例代码。
switch UIApplication.shared.statusBarOrientation {
    case .portrait:
      print("Home button in bottom")
    case .portraitUpsideDown
      print("Home button in top")
    case .landscapeLeft
      print("Home button in left")
    case .landscapeRight
      print("Home button in right")
    default:
      print("The interface may be rotating.")
    }

谢谢您的回答,但这完全不在我的掌握之中,因为这是客户的要求。如果支持纵向和横向方向,这可能是一个可行的解决方案,尽管需要大量的工作来支持两种方向上的视图。但目前并不能解决我面临的问题。 - iOSer
@iOSer 没问题。我们可以将这个答案保留给那些需要它的人。 - Vincent Sit

0
    if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {

        ...

    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {

        ...

    } else if UIDevice.current.orientation == UIDeviceOrientation.portrait {

        ...

    }
    else {

         ...
    }

你有没有想过使用 switch 语句? - Ashley Mills

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接