如何检测iOS设备的方向?

我有一个关于如何在iOS上检测设备方向的问题。 我不需要接收更改通知,只需要目前的方向。 这似乎是一个相当简单的问题,但是我一直没有把头围绕。 以下是我迄今为止所做的:

UIDevice *myDevice = [UIDevice currentDevice] ; [myDevice beginGeneratingDeviceOrientationNotifications]; UIDeviceOrientation deviceOrientation = myDevice.orientation; BOOL isCurrentlyLandscapeView = UIDeviceOrientationIsLandscape(deviceOrientation); [myDevice endGeneratingDeviceOrientationNotifications]; 

在我看来这应该工作。 我使设备能够接收设备定位通知,然后询问它是在什么方向,但它不工作,我不知道为什么。

真的很老的线程,但没有真正的解决scheme。

我有同样的问题,但发现得到的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 

如果UIViewController:

 if (UIDeviceOrientationIsLandscape(self.interfaceOrientation)) { // } 

如果UIView:

 if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { // } 

UIDevice.h:

 #define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown) #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight) 

更新

将此代码添加到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 

用法:

 if (isLandscape) { NSLog(@"Landscape"); } 

对于你要找的第一个你必须得到通知,如果方向改变! 你可以在viewDidLoad中设置这个东西

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

只要你的设备的方向改变OrientationDidChange调用,你可以做任何你想要的方向

 -(void)OrientationDidChange:(NSNotification*)notification { UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation]; if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight) { } else if(Orientation==UIDeviceOrientationPortrait) { } } 

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

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

至于你的例子,这应该工作。 当你说它不工作,你是什么意思? 它会给你什么结果与你的预期?

没有被“UIDeviceOrientation”所满足,因为当一个UIViewcontroller方向被固定到一个特定的方向时,你不会得到与设备方向有关的信息,所以正确的做法是使用“UIInterfaceOrientation”

你可以使用“self.interfaceOrientation”从UIViewController获得方向,但是当你分解我们的代码时,你可能需要在视图控制器之外进行这种testing(自定义视图,类别…),所以你仍然可以使用rootviewController访问控制器外部的任何信息:

 if (UIInterfaceOrientationIsLandscape(view.window.rootViewController.interfaceOrientation)) { } 

在Swift 3.0中

获取设备定位。

 /* return current device orientation. This will return UIDeviceOrientationUnknown unless device orientation notifications are being generated. */ UIDevice.current.orientation 

从您的应用获取设备定位

 UIApplication.shared.statusBarOrientation 

你是否解锁了设备方向的硬件锁? 有一个在我的iPad 1的边缘。

有一种方法可以通过使用来自CoreMotion的数据来实现这个方向locking是否启用 。 这是代码:

 #import <CoreMotion/CoreMotion.h> CMMotionManager *cm=[[CMMotionManager alloc] init]; cm.deviceMotionUpdateInterval=0.2f; [cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *data, NSError *error) { if(fabs(data.gravity.x)>fabs(data.gravity.y)){ NSLog(@"LANSCAPE"); if(data.gravity.x>=0){ NSLog(@"LEFT"); } else{ NSLog(@"RIGHT"); } } else{ NSLog(@"PORTRAIT"); if(data.gravity.y>=0){ NSLog(@"DOWN"); } else{ NSLog(@"UP"); } } }]; 

这里有一些Swiftvariables使检测更容易:

 let LANDSCAPE_RIGHT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight let LANDSCAPE_LEFT: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft let LANDSCAPE: Bool = LANDSCAPE_LEFT || LANDSCAPE_RIGHT let PORTRAIT_NORMAL: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait let PORTRAIT_REVERSE: Bool = UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown let PORTRAIT: Bool = PORTRAIT_REVERSE || PORTRAIT_NORMAL