iOS - 保持横向布局但更改控件

iOS - keep layout in landscape but change controls

我已经完成了我的 iOS 通用应用程序中的自动布局,它在纵向模式下运行得非常好。但是,我希望用户能够旋转设备并以横向模式玩游戏。我面临的问题是我根本不想改变布局,只改变游戏的控制(向上滑动屏幕应该让玩家在两个方向上都上升)。

问题是,我不知道如何防止方向改变布局,同时能够根据方向改变行为。你们知道我该怎么做吗?

找到了一种方法,以供将来参考,当方向被禁用时,我们仍然可以访问设备方向(而不是界面方向),并注册通知以根据更改采取行动。

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
        }
    }


}