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

30

我已经开发了一个iOS应用,并在iOS6设备上进行了测试。在测试过程中,我意识到我的应用程序没有按预期响应方向变化。

这是我的代码:

// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

确切地说,我想知道在iOS中方向改变时调用了哪些方法。


“我的代码在方向方面无法正常工作。”是什么意思? - Midhun MP
实际上,我想知道当我旋转设备时调用了哪个方法..? - Code Hunter
它将调用 shouldAutorotate 方法和 supportedInterfaceOrientations 方法。 - Midhun MP
@MidhunMP 这个方法在iOS 6中已经被弃用了...这就是原因。 - Code Hunter
这是一个误解,shouldAutoRotate方法是作为shouldAutorotateToInterfaceOrientation的替代引入的。它可以在iOS5和iOS6中使用。 - Midhun MP
显示剩余2条评论
10个回答

62

你可以尝试这个,可能会对你有帮助:

如何在iOS 6中编程更改设备方向

或者

Objective-c:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]

Swift:

->

Swift:

if UIDevice.current.orientation.isLandscape {
    // Landscape mode
} else {
    // Portrait mode
}

12
请注意,“方向”属性的值始终为0,除非通过调用“beginGeneratingDeviceOrientationNotifications”启用了方向通知。文档 - DanSkeel
请注意,这与获取ViewController方向不同,因为它可能处于横向模式,但UIDevice.current.orientation.isLandscape仍可能返回false。在我的情况下,UIDevice.current.orientation是.faceUp。 - Giohji

36

你可以尝试这个方法,它可以解决你的问题。

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

请参考以下链接了解应用程序扩展获取设备当前方向(应用程序扩展)


2
真的很糟糕的想法。不要挂钩那个属性。 - DanSkeel
2
有人可以设置这个属性statusBarOrientation,它将不再与真实的interfaceOrientation相同。通过此方法设置的状态栏方向不会因设备方向的改变而改变。 - DanSkeel
2
@DanSkeel 怎么可能。我们可以使用这个属性来获取精确的当前方向。如果我们强制设置状态栏方向,那么我们就有责任小心处理它。 - Girish
1
如果你编写了一些工具或框架,你无法控制所有的代码。因此,如果你想确保没有人这样做,最好避免使用它。在我看来。 - DanSkeel
2
我的意思是,你无法控制使用你的框架的整个应用程序的代码。因为你正在编写框架,而不是应用程序。开发人员可以进行设置,而你可能不会知道。 - DanSkeel
显示剩余2条评论

7
也许有些愚蠢,但在视图控制器中它是有效的(仅适用于ViewController):
if (self.view.frame.size.width > self.view.frame.size.height) {
    NSLog(@"Hello Landscape");
}

1
有趣而有效。 - Matt Mc

6
  @property (nonatomic) UIDeviceOrientation m_CurrentOrientation ;

/* 你需要在ViewDidload或ViewWillAppear中声明以下代码 */
- (void)viewDidLoad

   {

   [super viewDidLoad];

   [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

/* 现在,我们的设备会在改变方向时发出通知,因此您可以使用当前方向来控制您的代码或程序 */
- (void)deviceOrientationDidChange:(NSNotification *)notification

 {

 //Obtaining the current device orientation

 /* Where self.m_CurrentOrientation is member variable in my class of type UIDeviceOrientation */

 self.m_CurrentOrientation = [[UIDevice currentDevice] orientation];

    // Do your Code using the current Orienation 

 }

如何使用self.m_CurrentOrientation检查横屏和竖屏? - Sanju
如果 (UIDeviceOrientationIsLandscape(self.m_CurrentOrientation) { // 横向方向的代码 } - Vicky

2

Swift 5答案

订阅方向变化通知:

override func viewDidLoad() {
   super.viewDidLoad()
    
   NotificationCenter.default.addObserver(self, selector: #selector(orientationChangedFromNotification), name: UIDevice.orientationDidChangeNotification, object: nil)
}

处理通知:

@objc func orientationChangedFromNotification() {
   self.updateUIForOrientation()
}

处理方向变化:
func updateUIForOrientation() {
   let orientation = UIDevice.current.orientation
   
   //TODO: Your logic
}

注意:这将告诉您物理设备的方向。还可以访问应用程序的界面方向:

UIApplication.shared.delegate?.window?.windowScene?.interfaceOrientation

2

请按照UIDevice文档的说明操作。

需要调用

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

每次设备方向发生变化时,您将收到一个UIDeviceOrientationDidChangeNotification通知。


2

这篇教程提供了一个简单明了的概述,介绍如何使用UIDeviceOrientationDidChangeNotification来处理设备旋转。它应该能够帮助你理解如何使用UIDeviceOrientationDidChangeNotification在设备方向改变时得到通知。


1
UIDevice.current.orientation.isLandscape

接受的答案读取设备的方向,这可能与您的视图控制器的方向不同,特别是如果您的设备处于几乎水平的位置。要获取您的视图控制器的方向,请使用其interfaceOrientation属性,该属性自iOS 8.0以来已弃用,但仍然报告正确。

0
在您的 ViewController 中,您可以使用 didRotateFromInterfaceOrientation: 方法来检测设备何时被旋转,然后在每个方向上执行所需操作。
示例:
#pragma mark - Rotation

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case 1:
        case 2:
            NSLog(@"portrait");
            // your code for portrait...
            break;

        case 3:
        case 4:
            NSLog(@"landscape");
            // your code for landscape...
            break;
        default:
            NSLog(@"other");
            // your code for face down or face up...
            break;
    }
}

0
请注意,[UIDevice currentDevice] orientation]Application sharedApplication].statusBarOrientation已经过时。
使用deviceOrientationDidChange通知似乎是获取当前方向的最佳常规方法,但它在应用程序启动时不提供方向,这对我来说是个问题。
因此,在ViewDidLoad方法中,我读取屏幕尺寸以检测第一个方向:
CGSize __screenSize = [UIScreen];

但是在一定的延迟之前,屏幕尺寸是不可靠的,所以我通过在延迟1秒后读取屏幕尺寸来找到了解决方案,就像这样:
[self performSelector:@selector( readScreenSize ) withObject:nil afterDelay:1];

总结一下,对我来说,它的工作原理如下:

1/ 通过在短暂延迟后读取屏幕尺寸来检测第一个方向。

2/ 通过使用“deviceOrientationDidChange”通知来检测方向变化。

祝好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接