2017-08-18 134 views
0

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

} 

,并在customCameraViewviewDidLoad(),我锁定的方位设置为.landscapeLeft

AppUtility.lockOrientation(.landscapeLeft) 

留下我在我的viewWillDissapear方法设置此视图,以便解锁前方向:

AppUtility.lockOrientation(.all) 

的问题是,在customCameraView风景模式下工作,只有当设备的自动旋转启用(未锁定),当我回到前一个视图控制器时,它最初显示在landscapeLeft中,因此我必须将设备设置为纵向模式。此视图使用AppUtility扩展名以纵向方式锁定。

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

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

因此,对于我来说最完美的解决方案是始终使landscape32模式下的customCameraView和纵向上的前一个视图无论旋转是否被激活。

我在这个bug上奋斗了几天,现在有点帮助会很棒。

回答

1

可以申请下面的代码所要求的customCameraView控制器:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
    return UIInterfaceOrientationMask.landscapeLeft 
} 
+0

感谢你为这个。真正的解决方案是在.portrait模式下限制所有应用程序并覆盖customCameraView中的supportedInterfaceOrientations。也感谢nathan。 –

+0

根据我的经验,这具有很多限制,特别是在不同方向的视图控制器之间转换时。 – nathan

+0

@nathan这完美地适用于我,我只是在横向模式下设置应用程序,我只在横向模式下设置cameraView。我还将前一个控制器设置为纵向,在关闭cameraViewController后自动处于纵向模式。 –

1

iOS鼓励开发人员支持纵向和横向,因此可能很难限制应用程序的一部分为横向,而另一部分为纵向。

工作得很好的一个选项是在项目设置中将整个应用限制为纵向模式,并将旋转变换应用于任何需要横向的视图控制器。这样,视图看起来就像是风景,并且您可以完全控制视图的旋转。

相关问题