我正在使用 Xcode 7 和 Swift 2。我正在开发一个带有相机预览层和控件的界面,其显示方式类似于本机 iOS 相机应用程序。当您转动设备时,所有控件都保持在原位,但图标会“旋转”到位以根据设备方向正确定位。 (我希望我以一种有意义的方式解释了这一点。如果没有,请打开 iPhone 上的本机相机应用程序,然后转动设备几次,看看我在说什么。)
我已经通过使用以下方法修复整体界面方向来使基本界面正常工作:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeRight
}
然后我使用变换来旋转每个按钮以适应设备方向。
问题是:我需要能够在同一界面中呈现警报消息(UIAlertController)和共享界面(UIActivityViewController)。如何让这些项目旋转到正确的方向,同时仍保持界面的其余部分静态?
在我看来,这里有两种可能的方法——我只是不知道如何使其中一种起作用:
将界面设置为自动旋转并支持所有方向,但对我需要保持锁定到位的视图禁用自动旋转。
将界面设置为仅允许 .landscapeLeft (这就是当前的设置方式)并找到一种方法来旋转警报消息和共享对话框。
成功了。我需要访问presentedViewController.view 来旋转警报并共享视图。
//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))
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)