繁体   English   中英

如何知道iDevice的当前方向

[英]How to know the current orientation of a iDevice

我正在为数学课程和练习做一个应用程序...

我只编写了MainMenu,但是遇到了问题。 确实,我想知道目前我的工作方向。

但是,我尝试了不同的方法来了解当前的方向,但是我没有成功:

if UIDevice.current.orientation == UIDeviceOrientation.portrait {
            NSLog("Portrait")

        }

        else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight || UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
            NSLog ("Landscape")
        }

我也看到了这个答案,但是它不起作用(或者我不太了解): 在Swift中获取设备方向 ,bu

我想在ViewDidLoad()中指定我在UIViewController中。

感谢您的帮助

我用按钮设置了一个小的原型应用程序,该按钮可在按下时检查设备方向的变化。 这里是文档的链接,希望对您的项目有所帮助。

class ViewController: UIViewController {

    let device = UIDevice.current

    override func viewDidLoad() {
        super.viewDidLoad()
        device.beginGeneratingDeviceOrientationNotifications()
    }

    @IBAction func orientation(_ sender: UIButton) {
        if device.isGeneratingDeviceOrientationNotifications {
            switch device.orientation {
            case .landscapeLeft:
                print("Landscape Left")
            case .portrait:
                print("Portrait")
            case .landscapeRight:
                print("Right")
            case .portraitUpsideDown:
                print("Upside down")
            case .unknown:
                print("Unknown")
            default:
                print("Else")
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM