【问题标题】:Swift Detect Every Time Device Orientation Changes [closed]每次设备方向更改时快速检测 [关闭]
【发布时间】:2020-01-03 03:18:59
【问题描述】:

我正在使用最新版本的 Swift 在 Xcode 中创建一个 iPhone 游戏。为什么我问这个问题是当用户翻转手机并改变方向时,有什么方法可以检测到,所以我可以改变事物的放置、大小等?

【问题讨论】:

    标签: swift


    【解决方案1】:

    如果您想要支持的话,您可能需要研究约束,以便您的游戏在纵向和横向上看起来都不错。您可以在情节提要中设置约束,然后按改变特性并选择横向选项来设置新的横向约束。

    否则,如果您确实需要检查方向变化,您可以尝试:

    override func viewDidLoad() {
        super.viewDidLoad()        
    
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
    }
    
    deinit {
       NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)         
    }
    
    func rotated() {
        if UIDevice.current.orientation.isLandscape {
            print("Landscape")
        } else {
            print("Portrait")
        }
    }
    

    Source

    【讨论】:

    • 当电话方向被锁定时,此解决方案对我不起作用。知道我怎么能得到那个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多