向左或向右检测风景方向

问题描述 投票:0回答:1

我的应用支持纵向和横向->左右两边。我可以检测到它的风景。但无法检测到左或右。这是我的代码

if UIDevice.current.orientation.isLandscape {
// Do some task 
}

当用户旋转设备时,我需要检测用户是否向左旋转或向右旋转!

在上述情况下,我需要检查其左侧还是右侧。我如何检测到

谢谢

ios swift iphone xcode orientation
1个回答
0
投票

我认为您正在寻找类似的东西

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


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

    } else {
        //not landscape left or right
    }

编辑--------

根据您的评论,您正在寻找接口方向而不是设备方向。

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.current.orientation{
    case .portrait:
        text="Portrait"
    case .portraitUpsideDown:
        text="PortraitUpsideDown"
    case .landscapeLeft:
        text="LandscapeLeft"
    case .landscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}

上面的代码检测界面方向...注意switch语句如何仍然使用UIDeviceOrientation

下面是您可能想使用的另一种方法

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("landscape")
    } else {
        print("portrait")
    }
}

再次注意,仍然使用UIDevice Orientation ...

© www.soinside.com 2019 - 2024. All rights reserved.