设备方向为faceDown或faceUp时如何获取界面方向?

时间:2019-07-11 23:15:25

标签: ios swift

我通过获取当前设备的方向来设置<Grid> <textEditor:AvalonTextEditor Grid.Row="2" TextContainer="{Binding TextContainer}" ShowLineNumbers="True" FontFamily="Consolas" SyntaxHighlighting="XML" FontSize="10pt"> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseLeftButtonDown"> <i:InvokeCommandAction Command="{Binding MouseLeftButtonDownCommand}" /> </i:EventTrigger> <i:EventTrigger EventName="PreviewMouseLeftButtonUp"> <i:InvokeCommandAction Command="{Binding MouseLeftButtonUpCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </textEditor:AvalonTextEditor> </Grid> 的方向,但是当设备为faceDown或faceUp时,我很难获得界面方向。任何帮助将不胜感激。预先感谢

AVCaptureVideoOrientation

private func configureDeviceOrientation() { var videoOrientation: AVCaptureVideoOrientation { switch UIDevice.current.orientation { case .faceUp, .faceDown: //I don't know whether the interface orientation is landscapeLeft //or landscapeRight nor do I know how to check. } } 已从iOS 8弃用,这是一个框架,因此我无权访问interfaceOrientation

1 个答案:

答案 0 :(得分:0)

添加到viewWillAppear来检查设备是否旋转:

   //Observer for if Device Rotated
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(device_rotated),
                                            name:UIDevice.orientationDidChangeNotification,
                                               object: nil)

添加到viewWillDisappear:

 NotificationCenter.default.removeObserver(self)

添加到viewController。如果设备旋转或方向更改,则会调用该函数:

@objc func device_rotated(notification:Notification) -> Void{

    switch UIDevice.current.orientation {
    case .landscapeLeft, .landscapeRight:
        print("Landscape")
    case .portrait, .portraitUpsideDown:
        print("Portrait")
    case .faceUp:
        print("Face Up")
    case .faceDown:
        print("Face Down")
    default:
        print("Default")
    }

}