2017-01-27 47 views
-1

我完成了我的iOS通用應用程序中的自動佈局的東西,它的肖像完美工作。但是,我希望用戶能夠旋轉設備並以橫向模式播放遊戲。我面臨的問題是我不希望佈局改變,只改變遊戲的控制(向上滑動屏幕會使玩家在兩個方向上都會上升)。iOS - 保持風景佈局,但改變控制

事情是,我不知道如何防止方向改變佈局,同時能夠根據方向改變行爲。你們有什麼想法我可以如何管理?

回答

0

已找到一種方法來做爲將來參考,當方向被禁用時,我們仍然可以訪問設備方向(而不是接口方向),並註冊一個通知以便在更改時採取行動。

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


}