我正在开发一款摄影应用程序,可以在纵向或横向拍摄照片。由于项目需求,我不能让设备方向自动旋转,但需要支持旋转。
当使用以下方向方法时:
我能在启动时正确设置旋转。通过更改方向值并调用
编辑:
以下方法对我无效:
当使用以下方向方法时:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if self.orientation == .Landscape {
return UIInterfaceOrientationMask.LandscapeRight
} else {
return UIInterfaceOrientationMask.Portrait
}
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
if self.orientation == .Landscape {
return UIInterfaceOrientation.LandscapeRight
} else {
return UIInterfaceOrientation.Portrait
}
}
我能在启动时正确设置旋转。通过更改方向值并调用
UIViewController.attemptRotationToDeviceOrientation()
,我能够支持旋转到新的所需界面。然而,这种旋转只有在用户实际移动设备时才会发生。我需要它自动发生。
我能够调用:UIDevice.currentDevice().setValue(targetOrientation.rawValue, forKey: "orientation")
来强制更改,但这会引起其他副作用,因为UIDevice.currentDevice().orientation
仅从那时起返回setValue。(而且非常不好)
我错过了什么吗?我已经研究了关闭和启动一个新的视图控制器,但这会带来其他问题,比如在解除和立即呈现新的视图控制器时出现常见的UI故障。编辑:
以下方法对我无效:
编辑2:
对可能解决方案的思考:
直接设置方向(使用
setValue
),并处理在iOS 9上出现的所有副作用(不可接受)。我可以使用当前的解决方案,并指示用户需要旋转设备。一旦设备被物理旋转,UI将旋转并正确锁定在原位。(UI效果差)
我可以找到一种解决方案,强制刷新方向并在没有物理操作的情况下旋转。(这就是我的问题,并且正在寻找解决方案)
手动完成所有操作。我可以将界面锁定为纵向或横向,并手动旋转和调整容器视图的大小。这是“繁琐”的,因为它放弃了所有的尺寸类别自动布局功能,并导致代码更加冗长。我试着避免这种情况。