如何检测方向变化? [英] How to detect orientation change?

查看:271
本文介绍了如何检测方向变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift,我希望能够在旋转到横向时加载UIViewController,任何人都可以指向正确的方向吗?

I am using Swift and I want to be able to load a UIViewController when I rotate to landscape, can anyone point me in the right direction?

我可以'在网上找到任何东西,有点困惑的文件。

I Can't find anything online and a little bit confused by the documentation.

推荐答案

以下是我如何运作:

AppDelegate.swift 里面的 didFinishLaunchingWithOptions 函数我把:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

然后在AppDelegate类中我放了以下函数:

and then inside the AppDelegate class I put the following function:

func rotated() {
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
        print("Landscape")
    }

    if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
        print("Portrait")
    }

}

希望这可以帮助其他人!

Hope this helps anyone else!

谢谢!

这篇关于如何检测方向变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆