简体   繁体   English

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

[英]How to get the interface orientation when the device orientation is faceDown or faceUp?

I'm setting the orientation for AVCaptureVideoOrientation by getting the current device orientation, however I'm having trouble to get the interface orientation when the device is faceDown or faceUp.我通过获取当前设备方向来设置AVCaptureVideoOrientation的方向,但是当设备面朝下或面朝上时,我无法获得界面方向。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks in advance提前致谢

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.
    }
}

interfaceOrientation is deprecated from iOS 8 and this is a framework, therefore I don't have access to UIApplication.shared interfaceOrientation已从 iOS 8 弃用,这是一个框架,因此我无权访问UIApplication.shared

Add to viewWillAppear to check if device is rotated: 添加到viewWillAppear来检查设备是否旋转:

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

Add to viewWillDisappear: 添加到viewWillDisappear:

 NotificationCenter.default.removeObserver(self)

Add to viewController. 添加到viewController。 Function will be called if device is rotated or orientation changed: 如果设备旋转或方向更改,则会调用该函数:

@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")
    }

}

Use self.view.window?.windowScene?.interfaceOrientation in place of UIDevice.current.orientation .使用self.view.window?.windowScene?.interfaceOrientation代替UIDevice.current.orientation

This value will be either portrait , portraitUpsideDown , landscapeRight , or landscapeLeft .该值将是portraitportraitUpsideDownlandscapeRightlandscapeLeft

See here for documentation.有关文档,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM