简体   繁体   English

如何以编程方式获取设备的当前方向?

[英]How to get current orientation of device programmatically?

I want to know the device orientation, I used 我想知道我使用的设备方向

 UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation

but this only works when I change the orientation otherwise it does not give anything (TRUE/FALSE).So how can I get current device orientation even when orientation is not changed. 但这仅在我改变方向时有效,否则它不会给出任何东西(TRUE / FALSE)。那么即使方向没有改变,我怎样才能获得当前的设备方向。

Thanks in advance! 提前致谢!

To get orientation 获得方向

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

and the possible orientations are 并且可能的方向是

typedef enum {
  UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeLeft,
  UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

Gives an Implicit Enumeration Warning. 给出隐含的枚举警告。

Use this instead: 请改用:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Sorry if you've already done this, but have you looked into the beginGeneratingDeviceOrientationNotifications method, which enables notification generation for the device? 很抱歉,如果您已经这样做了,但是您是否已查看了beginGeneratingDeviceOrientationNotifications方法,该方法可以为设备生成通知?

Also, are you looking for the device orientation or the interface orientation? 此外,您在寻找设备方向还是界面方向? If you're looking for a simpel Portrait vs Landscape orientation info, you should look into the interfaceOrientation of the view controller. 如果您正在寻找一个简单的纵向与横向方向信息,您应该查看视图控制器的interfaceOrientation。

Hope this helps! 希望这可以帮助!

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

相关问题 获取当前设备方向 - Get the current device orientation 即使设备的方向锁定,也获取当前的 iOS 设备方向 - Get current iOS device orientation even if device's orientation locked 获取设备当前方向(应用扩展) - Get device current orientation (App Extension) 如何以编程方式设置设备(UI)方向? - How to set device (UI) orientation programmatically? 如何在iOS 6中以编程方式更改设备方向 - How to change the device orientation programmatically in iOS 6 如何在 Swift 中以编程方式向项目添加设备方向? - How to add device orientation to project programmatically in Swift? 如何在 Swift 中以编程方式更改设备方向? - How to change Device Orientation programmatically in Swift? 当前方向为面朝上或面朝下时如何将设备方向更改为纵向 - How to change the device orientation to portrait when the current orientation is faceup or facedown 如何在iOS中基于设备方向以编程方式创建视图? - How do I programmatically create views based on device orientation in iOS? 如何在以编程方式创建的设备方向上更改 UIView 子类的框架? - How to change the frame of a UIView subclass on device orientation which is programmatically created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM