iOS:加载时的设备方向

看来,当我的应用程序加载,它不知道它的当前方向:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationPortrait) { NSLog(@"portrait");// only works after a rotation, not on loading app } 

一旦我旋转设备,我得到一个正确的方向,但是当我加载的应用程序,而不改变方向,似乎使用[[UIDevice currentDevice] orientation]不知道当前的方向。

当我第一次加载我的应用程序时,有另一种方法来检查这个吗?

编辑:我错读你的问题。 这将允许您以某种方向启动您的应用程序。 刚刚意识到你正在试图找出发射的方向。

有一种方法来检查UIApplication上的状态栏方向:

 [[UIApplication sharedApplication] statusBarOrientation]; 

原始答案

尝试在plist文件中设置应用程序接受的设备方向:

 <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> 

这将表明您的应用程序支持肖像(底部的主页button),左侧风景和右侧风景。

然后,在您的UIViewControllers中,您将需要重写shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)方法以在应用程序旋转时返回YES:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight; } 

这将告诉UIViewController自动旋转,如果设备是在你支持的方向之一。 如果您还想支持倒置的方向(顶部带有Homebutton的人像),那么将其添加到您的plist中,然后将此方法返回YES。

让我们知道它是如何工作的。

我认为这将工作:

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation; 

根据UIDevice的参考:
引用:
“除非方向通知已通过调用beginGeneratingDeviceOrientationNotifications启用,否则此属性的值总是返回0。
我最初认为这个财产总是包含当前的方向,但显然不是这样。 我猜想,在通常访问orientation属性的其他情况下,我们正在为后台处理启用通知,因此,不需要在应用程序代理中手动完成

有一件事没有人涉及到,就是你将UIDeviceOrientationtypes存储在一个UIInterfaceOrientationvariables中。 他们是不同的,不应该被视为平等的。 请注意, UIDeviceOrientationLeft等于UIInterfaceOrientationRight (因为与设备相比,接口旋转方向相反)。

你可以通过在里面插入下面的通知来做到这一点

 -(void)viewDidLoad [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 

然后把下面的方法放在你的类中

 -(void)checkRotation:(NSNotification*)notification { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { //Do your textField animation here } } 

上面的方法将检查ipad或iPhone的状态栏的方向,并根据它来做你的animation在所需的方向。

在加载时,设备方向可以是.Unknown.FaceUp 。 要确定它是纵向还是横向,我使用statusBarOrientation作为备份,如下所示:

  var portraitOrientation = UIDevice.currentDevice().orientation == .Portrait if UIDevice.currentDevice().orientation == .Unknown || UIDevice.currentDevice().orientation == .FaceUp { portraitOrientation = UIApplication.sharedApplication().statusBarOrientation == .Portrait } 

这样我可以确保portraitOrientation总是告诉我,如果设备是纵向模式,如果不是,它将在横向。 即使在第一次加载应用程序。

问题是[UIDevice currentDevice]orientation]有时会错误地报告设备的方向。

而是使用[[UIApplication sharedApplication]statusBarOrientation]这是一个UIInterfaceOrientation所以检查它,你将需要使用UIInterfaceOrientationIsLandscape(orientation)

希望这可以帮助。

为了从状态栏获得方向,在plist文件中启用所有的方向也是很重要的。

Swift 3基于@Marjin的代码。

 var portraitOrientation = UIDevice.current.orientation == .portrait if UIDevice.current.orientation == .unknown || UIDevice.current.orientation == .faceUp { portraitOrientation = UIApplication.shared.statusBarOrientation == .portrait } if(portraitOrientation) { // Portrait } else { } 

尝试使用加速度计来读取数据,使用UIA加速度计,获得共享加速度计,设置其代表,获取读数,从那里找出方向。

尝试了所有并没有好的结果。 所以我做了什么,因为我在ipad上,把所有的工作都留给了splitViewController方法来使barButton无效:

对于肖像:

 - (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc { NSlog(@"portrait");} 

对于景观:

 - (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ NSlog(@"landscape");} 

这总是在加载工作。

我仍然使用iPhone 4的这个工作代码片段:

 -(void)deviceOrientationDidChange:(NSNotification *)notification{ //Obtaining the current device orientation UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; int value = 0; if(orientation == UIDeviceOrientationPortrait) { value = 0; }else if(orientation == UIDeviceOrientationLandscapeLeft) { value = 90; }else if(orientation == UIDeviceOrientationLandscapeRight) { value = -90; } CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value)); [photoImageView setTransform:cgCTM]; 

}

这是真正的答案。 当一个应用程序启动,它的方向是未知的。 它使用shouldAutorotateToInterfaceOrientation和supportedInterfaceOrientations来决定select哪个方向。

当我在iPhone 5.0模拟器中启动一个示例应用程序时,请使用下面的代码和“支持的接口方向”旋转它,并使用所有4种可能的方向:

 20:44:08.218 RotationTestApp Supported orientation: Portrait 20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down) 20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right) 20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left) 20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait) 20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait) 20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait) 20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait) 20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft) 20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft) 20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown) 20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown) 20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight) 20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight) 20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown) 

我看过很多代码片段,但是它们都不够普遍(iPad和iPhone,iOS 5.0+)。

而不是摸索尝试这个尝试,把以下内容放在你的root vc中:

 #define ToNSString_BEGIN(T) \ NSString* T##ToNSString(T valueParameter) { \ switch (valueParameter) { #define ToNSString_VALUE(value) \ case value: return @#value #define ToNSString_END(T) \ } \ return @"(unknown)"; \ } // NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation); ToNSString_BEGIN(UIInterfaceOrientation); ToNSString_VALUE(UIInterfaceOrientationPortrait); // 1 ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2 ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft); // 3 ToNSString_VALUE(UIInterfaceOrientationLandscapeRight); // 4 ToNSString_END (UIInterfaceOrientation); // NSString* UIDeviceOrientationToNSString(UIDeviceOrientation); ToNSString_BEGIN(UIDeviceOrientation); ToNSString_VALUE(UIDeviceOrientationUnknown); // 0 ToNSString_VALUE(UIDeviceOrientationPortrait); // 1 ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown); // 2 ToNSString_VALUE(UIDeviceOrientationLandscapeLeft); // 3 ToNSString_VALUE(UIDeviceOrientationLandscapeRight); // 4 ToNSString_VALUE(UIDeviceOrientationFaceUp); // 5 ToNSString_VALUE(UIDeviceOrientationFaceDown); // 6 ToNSString_END (UIDeviceOrientation); // Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms. - (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation { NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation); return interfaceOrientationAsMask & [self supportedInterfaceOrientations]; } // Reads from the project's-Info.plist - (NSUInteger)supportedInterfaceOrientations { static NSUInteger orientationsResult; if (!orientationsResult) { NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"]; for (id orientationString in supportedOrientations) { if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) { orientationsResult |= UIInterfaceOrientationMaskPortrait; NSLog(@"Supported orientation: Portrait"); } else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) { orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown; NSLog(@"Supported orientation: Portrait (upside-down)"); } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) { orientationsResult |= UIInterfaceOrientationMaskLandscapeRight; NSLog(@"Supported orientation: Landscape (home button on the left)"); } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) { orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft; NSLog(@"Supported orientation: Landscape (home button on the right)"); } else { NSLog(@"Unrecognized orientation '%@' in mainBundle plist, key UISupportedInterfaceOrientations", orientationString); } } } return orientationsResult; } // iOS 6+ (not yet used in 6.0.1) - (BOOL)shouldAutorotate { UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation; BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice]; NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice); NSLog(@"shouldAutorotate: %s (current orientation %@)", result ? "YES" : "NO", currentDeviceOrientation); return result; } // iOS 2.0 - 5.1 (iOS 6+ deprecated, 6.0.1 still works) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { NSString* orientationString; UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation; if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) { orientationString = [NSString stringWithFormat:@"current device orientation: %@, interface orientation wants: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice), UIInterfaceOrientationToNSString(interfaceOrientation) ]; } else { orientationString = [NSString stringWithFormat:@"device orientation: %@", UIDeviceOrientationToNSString(interfaceOrientationFromDevice) ]; } BOOL result = [self allowAutoRotate:interfaceOrientation]; NSLog(@"shouldAutorotateToInterfaceOrientation: %s (%@)", result ? "YES" : "NO", orientationString); return result; } 

不使用当前的方向仍然有一个唠叨的继续animation问题。 我的猜测是,每个VC的子类化和推动/通知代表popup的方向将是要走的路。

同样重要:

shouldAutorotateToInterfaceOrientation不起作用

tabBarController和横向模式下的navigationControllers,第二集

试试这个。 这对我有用。 粗糙的那个时候没有完成的启动方法没有检测到设备的方向。 它默认为一个肖像。 所以。 我正在使用检查统计栏的方向。 我testing这个代码。 把它放在appdeleget中的didfinishedlaunch方法中。

UIInterfaceOrientation orientation = [UIApplication sharedApplication] .statusBarOrientation;

 if(orientation == 0) {//Default orientation //UI is in Default (Portrait) -- this is really a just a failsafe. NSLog("for portrait"); }else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { NSLog("portrait"); }else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { NSLog("Landscap"); } 

试试这个[[UIApplication sharedApplication] statusBarOrientation];

或者在应用程序委托中实现这个

 (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; } 

有用

上面的每个人都发布了非常有效的答案:但作为一个更新:苹果的:你应该使用UIStatusBar方向来读取设备的当前方向:

您可以通过在viewDidLoad方法中使用int值来检查设备的当前方向:

  int orientationType = [[UIDevice currentDevice] orientation]; 

在哪里考虑以下几点。 。 。 – 1 =纵向(向上) – 2 =纵向朝下 – 3 =横向(向右) – 4 =横向(向左)

然后你可以使用IF语句在检测到方向后调用方法,等等等等。

希望这对某人有些帮助