所以我正在使用Swift 3和Xcode 8.3.3在iOS 10应用程序上工作。 在我的应用程序中,我必须连续拍摄8张带有指示灯的照片。Swift手动旋转视图控制器
因此,我使用AVCaptureVideoPreviewLayer
作为自定义相机,我需要设置在横向模式下显示相机的视图。 我用它在搜索问题时在stackoverflow上找到了这个扩展名。
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
,并在customCameraView
的viewDidLoad()
,我锁定的方位设置为.landscapeLeft
:
AppUtility.lockOrientation(.landscapeLeft)
留下我在我的viewWillDissapear
方法设置此视图,以便解锁前方向:
AppUtility.lockOrientation(.all)
的问题是,在customCameraView
风景模式下工作,只有当设备的自动旋转启用(未锁定),当我回到前一个视图控制器时,它最初显示在landscapeLeft中,因此我必须将设备设置为纵向模式。此视图使用AppUtility
扩展名以纵向方式锁定。
所以我想过总是通过覆盖shouldAutoRotate
var来激活自动旋转,但当设备自动旋转被锁定时它不起作用。
然后我想确保在打开相机之前启用了自动旋转功能,但是在stackoverflow上有100个人说这是不可能的。
因此,对于我来说最完美的解决方案是始终使landscape32模式下的customCameraView
和纵向上的前一个视图无论旋转是否被激活。
我在这个bug上奋斗了几天,现在有点帮助会很棒。
感谢你为这个。真正的解决方案是在.portrait模式下限制所有应用程序并覆盖customCameraView中的supportedInterfaceOrientations。也感谢nathan。 –
根据我的经验,这具有很多限制,特别是在不同方向的视图控制器之间转换时。 – nathan
@nathan这完美地适用于我,我只是在横向模式下设置应用程序,我只在横向模式下设置cameraView。我还将前一个控制器设置为纵向,在关闭cameraViewController后自动处于纵向模式。 –