如何在iOS上检测设备的方向?

Jus*_*anX 71 objective-c orientation uidevice ios

我有一个关于如何在iOS上检测设备方向的问题.我不需要接收更改通知,只需要接收当前方向.这似乎是一个相当简单的问题,但我无法绕过它.以下是我到目前为止所做的事情:

UIDevice *myDevice = [UIDevice currentDevice] ;
[myDevice beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = myDevice.orientation;
BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation);
[myDevice endGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)

在我看来这应该工作.我启用设备接收设备方向通知,然后询问它的方向,但它不起作用,我不知道为什么.

Moe*_*Moe 119

真的老线程,但没有真正的解决方案.

我有同样的问题,但发现获取UIDeviceOrientation并不总是一致的,所以改为使用:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right
Run Code Online (Sandbox Code Playgroud)

  • 如果可以的话,这是最正确的答案.这样,您只需选中此属性即可随时获取界面方向.请注意,状态栏是否隐藏无关紧要,无论如何都会更新属性! (5认同)
  • 如果应用程序的方向仅限于纵向模式或横向模式,则此功能无效. (4认同)
  • 如果你挑剔并选择不依赖故障保险,请不要忘记`UIInterfaceOrientationPortraitUpsideDown`. (3认同)

TON*_*y.W 77

如果UIViewController:

if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
    // 
}
Run Code Online (Sandbox Code Playgroud)

如果UIView:

if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
    //
}
Run Code Online (Sandbox Code Playgroud)

UIDevice.h:

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
Run Code Online (Sandbox Code Playgroud)

更新:

将此代码添加到xxx-Prefix.pch,然后您可以在任何地方使用它:

// check device orientation
#define dDeviceOrientation [[UIDevice currentDevice] orientation]
#define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
#define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
#define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
Run Code Online (Sandbox Code Playgroud)

用法:

if (isLandscape) { NSLog(@"Landscape"); }
Run Code Online (Sandbox Code Playgroud)


Sau*_*ati 22

对于您首先要寻找的内容,如果方向发生变化,您必须获得通知!你可以在viewDidLoad中设置This Thing

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

每当您的设备的方向改变时,OrientationDidChange被调用,您可以按照每个方向执行任何操作

-(void)OrientationDidChange:(NSNotification*)notification
{
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation];

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight)
    {
    }
    else if(Orientation==UIDeviceOrientationPortrait)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)


raf*_*tta 22

如果您想直接从加速度计使用获取设备方向[[UIDevice currentDevice] orientation].但是,如果您需要当前的应用程序方向(界面方向)使用[[UIApplication sharedApplication] statusBarOrientation].


Jas*_*ien 9

UIViewController有一个interfaceOrientation属性,您可以访问该属性以查找视图控制器的当前方向.

至于你的例子,这应该工作.当你说它不起作用时,你的意思是什么?它给你的结果与你的预期结果有什么关系?


Ash*_*k R 8

在Swift 3.0中

获取设备方向.

/* return current device orientation.
   This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated. 
*/
UIDevice.current.orientation
Run Code Online (Sandbox Code Playgroud)

从您的应用程序获取设备方向

UIApplication.shared.statusBarOrientation
Run Code Online (Sandbox Code Playgroud)

  • “‘statusBarOrientation’在 iOS 13.0 中已弃用:请改用窗口场景的 interfaceOrientation 属性。” 这是参考/sf/ask/1805758181/ (2认同)