ios - 当设备方向为 faceDown 或 faceUp 时如何获取界面方向?
问题描述
我AVCaptureVideoOrientation
通过获取当前设备方向来设置方向,但是当设备为 faceDown 或 faceUp 时,我无法获取界面方向。任何帮助将不胜感激。提前致谢
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
已从 iOS 8 弃用,这是一个框架,因此我无权访问UIApplication.shared
解决方案
我的AVCaptureVideoOrientation
.
只需使用self.view.window?.windowScene?.interfaceOrientation
代替UIDevice.current.orientation
:
switch self.view.window?.windowScene?.interfaceOrientation {
case .portrait:
previewLayer.connection?.videoOrientation = .portrait
case .portraitUpsideDown:
previewLayer.connection?.videoOrientation = .portraitUpsideDown
case .landscapeRight:
previewLayer.connection?.videoOrientation = .landscapeRight
case .landscapeLeft:
previewLayer.connection?.videoOrientation = .landscapeLeft
default:
print("other")
}
此值将是纵向、纵向向上、横向右或横向左。
在启动时以及每次设备改变方向时调用此代码。
有关文档,请参见此处。
推荐阅读
- python - 在 Python 中使用 For 循环并行循环
- python-3.x - 如何在表格中正确显示汉字
- javascript - 如何修复无法读取未定义错误的属性“currentTarget”
- ios - 如何对 largeTitleTextAttributes 颜色属性执行平滑过渡
- sql - DB Cursor FETCH NEXT 可以工作,但 FETCH FIRST 不能
- python - 计算列的平均值 - TypeError:“方法”对象不可下标
- wpf - 如何在 GMap.NET 中使用 NetTopologySuite 的 R-Tree 显示丰富的标记 WPF
- javascript - 使用页面上的添加按钮在 codeigniter 中添加另一个 ckeditor
- vue.js - 尝试编辑使用 vue 编写的其他人的代码:{{ }} 小胡子内的属性值在哪里?
- c++ - STL 容器如何跟踪容器的当前大小超过总大小?