我正在使用Xcode 7和Swift 2。我正在制作一个带有摄像头预览层和控件的界面,其显示方式与原生iOS摄像头应用类似。当你转动设备时,这些控件都保持在原地,但图标"枢轴"的位置是为了正确地定位设备方向。(我希望我以一种有意义的方式解释了这一点。如果没有,打开iPhone上的原生摄像头应用,把设备转几圈,看看我在说什么。
我已经通过使用
固定了整个界面的方向,使基本的界面工作:override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeRight
}
然后我使用transform来旋转每个按钮以适应设备的方向。
问题是:我需要能够呈现警报消息(UIAlertController)和共享接口(UIActivityViewController)在这个相同的接口。我如何让这些项目旋转到正确的方向,同时仍然保持界面的其余部分是静态的?
在我看来,这里有两种可能的方法——我只是不知道如何使其中任何一种工作:
将界面设置为自动旋转并支持所有方向,但禁用我需要锁定的视图的自动旋转
将界面设置为只允许。landscape left(这是目前的设置方式),并找到一种方法来旋转警告消息和共享对话框。
搞定了。我需要访问presenttedviewcontroller。视图来旋转警报和共享视图。
//used to periodically trigger a check of orientation
var updateTimer: NSTimer?
//Checks if the device has rotated and, if so, rotates the controls. Also prompts the user that portrait orientation is bad.
func checkOrientation(timer: NSTimer) {
//Array of the views that need to be rotated as the device rotates
var viewsToRotate = [oneView, anotherView]
//This adds the alert or sharing view to the list, if there is one
if let presentedVC = presentedViewController?.view {
viewsToRotate.append(presentedVC)
}
//Rotate all of the views identified above
for viewToRotate in viewsToRotate {
switch UIDevice.currentDevice().orientation {
case UIDeviceOrientation.Portrait:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
case UIDeviceOrientation.PortraitUpsideDown:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
case UIDeviceOrientation.LandscapeRight:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(2 * M_PI_2))
default:
viewToRotate.transform = CGAffineTransformMakeRotation(CGFloat(0))
}
}
}