[英]How to know the current orientation of a iDevice
I'm doing an app for Mathematics lessons & exercices... 我正在为数学课程和练习做一个应用程序...
I have only coded the MainMenu, but I've got a problem. 我只编写了MainMenu,但是遇到了问题。 Indeed, I would like to know the current orientation to set up my stuff.
确实,我想知道目前我的工作方向。
However, I tried different ways to know the current orientation, but I didn't succeed : 但是,我尝试了不同的方法来了解当前的方向,但是我没有成功:
if UIDevice.current.orientation == UIDeviceOrientation.portrait {
NSLog("Portrait")
}
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight || UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
NSLog ("Landscape")
}
I also see this answer, but it's not working (or maybe I haven't understood well) : Getting device orientation in Swift , bu 我也看到了这个答案,但是它不起作用(或者我不太了解): 在Swift中获取设备方向 ,bu
I would like to specify that I'm in UIViewController in the ViewDidLoad(). 我想在ViewDidLoad()中指定我在UIViewController中。
Thanks for your help 👨💻 感谢您的帮助
I set up a little prototype app with a button which checks for device orientation changes when pressed. 我用按钮设置了一个小的原型应用程序,该按钮可在按下时检查设备方向的变化。 Here is a link to the documentation which hopefully helps further your project along.
这里是文档的链接,希望对您的项目有所帮助。
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.