【发布时间】:2017-01-27 12:34:29
【问题描述】:
我已经完成了 iOS 通用应用程序中的自动布局功能,并且它在纵向模式下运行良好。但是,我希望用户能够旋转设备并以横向模式玩游戏。我面临的问题是我根本不想改变布局,只改变游戏的控件(向上滑动屏幕应该让玩家在两个方向上都向上)。
问题是,我不知道如何防止方向改变布局,同时能够根据方向改变行为。你们知道我该怎么做吗?
【问题讨论】:
标签: ios iphone layout uikit orientation
我已经完成了 iOS 通用应用程序中的自动布局功能,并且它在纵向模式下运行良好。但是,我希望用户能够旋转设备并以横向模式玩游戏。我面临的问题是我根本不想改变布局,只改变游戏的控件(向上滑动屏幕应该让玩家在两个方向上都向上)。
问题是,我不知道如何防止方向改变布局,同时能够根据方向改变行为。你们知道我该怎么做吗?
【问题讨论】:
标签: ios iphone layout uikit orientation
是否找到了一种方法,以供将来参考,当禁用方向时,我们仍然可以访问设备方向(而不是界面方向),并注册通知以根据更改采取行动。
class ViewController: UIViewController {
var currentOrientation = 0
override func viewDidLoad() {
super.viewDidLoad()
// Register for notification about device orientation change
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate(notification:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
// Remove observer on window disappears
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
if UIDevice.current.isGeneratingDeviceOrientationNotifications {
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
// That part gets fired on orientation change, and I ignore states 0 - 5 - 6, respectively Unknown, flat up facing and down facing.
func deviceDidRotate(notification: NSNotification) {
if (UIDevice.current.orientation.rawValue < 5 && UIDevice.current.orientation.rawValue > 0) {
self.currentOrientation = UIDevice.current.orientation.rawValue
}
}
}
【讨论】: