代码之家  ›  专栏  ›  技术社区  ›  iOSer

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

  •  1
  • iOSer  · 技术社区  · 6 年前

    作为我目前正在开发的应用程序的一部分,我需要单击一个图像,然后以正确的方向显示生成的图像。

    请记住,用户可以在任何可用方向(纵向、横向左/右、面朝上)单击图像。我能够实现所有模式的预期结果,但面朝上的方向。

    我的问题是: 当设备正面朝上时,有没有办法检测我的主页按钮会在哪一侧?如果是这样的话,我怎样才能实现同样的目标? 请注意,该应用程序是仅用于肖像的应用程序。所以我想,使用状态栏定位几乎是不可能的。

    期待听到你的想法。

    3 回复  |  直到 6 年前
        1
  •  2
  •   regina_fallangi BigFab    6 年前

    你可以做:

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

    还有 faceDown , faceUp ,等等。 检查 documentation 了解更多信息。

        2
  •  0
  •   Vincent Sit    6 年前

    根据你的需要,你应该使用 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.")
        }
    
        3
  •  0
  •   Ali Ihsan URAL    6 年前
        if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
    
            ...
    
        } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
    
            ...
    
        } else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
    
            ...
    
        }
        else {
    
             ...
        }