繁体   English   中英

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

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

我想知道我使用的设备方向

 UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation

但这仅在我改变方向时有效,否则它不会给出任何东西(TRUE / FALSE)。那么即使方向没有改变,我怎样才能获得当前的设备方向。

提前致谢!

获得方向

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

并且可能的方向是

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

给出隐含的枚举警告。

请改用:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

很抱歉,如果您已经这样做了,但是您是否已查看了beginGeneratingDeviceOrientationNotifications方法,该方法可以为设备生成通知?

此外,您在寻找设备方向还是界面方向? 如果您正在寻找一个简单的纵向与横向方向信息,您应该查看视图控制器的interfaceOrientation。

希望这可以帮助!

暂无
暂无

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

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