【问题标题】:Swift manually rotate view controllerSwift 手动旋转视图控制器
【发布时间】:2017-08-18 18:55:33
【问题描述】:

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

}

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

AppUtility.lockOrientation(.landscapeLeft)

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

AppUtility.lockOrientation(.all)

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

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

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

所以对我来说,完美的解决方案是始终将 customCameraView 设为 LandscapeLeft 模式,而将之前的视图设为纵向模式,无论是否激活了旋转。

我已经在这个错误上苦苦挣扎了好几天了,如果有一点帮助会很棒。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return UIInterfaceOrientationMask.landscapeLeft
      }
      

      【讨论】:

      • 谢谢你。真正的解决方案是将所有应用程序限制为 .portrait 模式并覆盖 customCameraView 中的 supportedInterfaceOrientations。也感谢内森。
      • 根据我的经验,这有很多限制,尤其是在不同方向的视图控制器之间转换时。
      • @nathan 这对我来说非常适合,我只是将应用程序设置为横向模式,而我只将 cameraView 设置为横向模式。我还将以前的控制器设置为纵向,以便在关闭 cameraViewController 后自动处于纵向模式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多