Swift 手动旋转视图控制器

Swift manually rotate view controller

所以我正在使用 Swift 3 和 Xcode 8.3.3 开发 iOS 10 应用程序。 在我的应用程序中,我必须连续拍摄 8 张带有相机指示器的照片。

所以我使用 AVCaptureVideoPreviewLayer 作为自定义相机,我需要设置在横向模式下显示相机的视图。 我使用我在搜索问题时在 Whosebug 上找到的这个扩展来做到这一点。

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

}

并且在 customCameraViewviewDidLoad() 中,我将锁定方向设置为 .landscapeLeft :

AppUtility.lockOrientation(.landscapeLeft)

在离开视图之前,我在 viewWillDissapear 方法中进行了设置以解锁方向:

AppUtility.lockOrientation(.all)

问题是 customCameraView 中的横向模式仅在启用设备的自动旋转(未锁定)时才有效,并且当我返回到之前的视图控制器时,它最初显示在landscapeLeft 所以我必须转动设备以将其置于纵向模式。此视图使用 AppUtility 扩展以纵向方式锁定。

所以我想总是通过覆盖 shouldAutoRotate var 来激活自动旋转,但是当设备自动旋转被锁定时它不起作用。

然后我想确保在打开相机之前启用自动旋转,但是 Whosebug 上的 100 个人都说这是不可能的。

所以对我来说,完美的解决方案是始终将 customCameraView 设置为横向左视图,将上一个视图设置为纵向视图,无论旋转是否激活。

几天来我一直在为这个错误而苦苦挣扎,如果能有一点帮助就好了。

iOS 鼓励开发者同时支持纵向和横向,因此很难将应用的一部分限制为横向而另一部分为纵向。

一个效果很好的选项是在项目设置中将整个应用程序限制为纵向模式,并对需要横向的任何视图控制器应用旋转变换。这样,视图的外观和行为就像横向一样,并且您可以完全控制视图的旋转。

能否在所需的 customCameraView 控制器上应用以下代码:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeLeft
}