Swift手动旋转视图控制器

时间:2017-08-18 18:55:33

标签: ios swift3 screen-orientation screen-rotation xcode8

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

所以我使用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")
    }

}

并在viewDidLoad()的{​​{1}}中,我将方向锁定为customCameraView

.landscapeLeft
离开视图之前

我在我的AppUtility.lockOrientation(.landscapeLeft) 方法中进行设置以解锁方向:

viewWillDissapear

问题是AppUtility.lockOrientation(.all) 中的横向模式仅在启用(未锁定)设备的自动旋转时才有效,当我回到上一个视图控制器时,它最初显示在landscapeLeft中我必须将设备转为纵向模式。此视图使用customCameraView扩展名以纵向方式锁定。

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

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

因此,对我而言,完美的解决方案是始终在landscapeLeft模式下制作shouldAutoRotate,在纵向中制作前一个视图,无论旋转是否被激活。

我现在已经在这个问题上苦苦挣扎了几天,一点帮助就会很棒。

2 个答案:

答案 0 :(得分:2)

iOS鼓励开发人员同时支持纵向和横向,因此很难将应用程序的某个部分限制为横向,而另一部分则是纵向。

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

答案 1 :(得分:1)

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

 select to_char(sysdate,'MON') from dual