如何获取iPhone当前的方向?

发布于 2024-08-27 07:28:42 字数 402 浏览 3 评论 0原文

有没有特殊的方法来获取 iPhone 的方向?我不需要它的度数或弧度,我希望它返回一个 UIInterfaceOrientation 对象。我只需要它来进行 if-else 结构,例如

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

提前致谢!

Is there a special method to get iPhones orientation? I don't need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else construction like

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

書生途 2024-09-03 07:28:42

这很可能是您想要的:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

然后您可以使用系统宏,例如:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{

}

如果您想要设备方向使用:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

这包括诸如 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown 之类的枚举

This is most likely what you want:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

You can then use system macros like:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{

}

If you want the device orientation use:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

This includes enumerations like UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown

も星光 2024-09-03 07:28:42

正如其他答案中所讨论的,您需要 interfaceOrientation,而不是 deviceOrientation。

最简单的方法是使用 UIViewController 上的 interfaceOrientation 属性。 (所以大多数情况下,只需: self.interfaceOrientation 就可以了)。

可能的值有:

UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

请记住:
通过将设备向右转动即可进入左方向。

As discussed in other answers, you need the interfaceOrientation, not the deviceOrientation.

The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self.interfaceOrientation will do).

Possible values are:

UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

Remember:
Left orientation is entered by turning your device to the right.

贱人配狗天长地久 2024-09-03 07:28:42

这是我必须编写的一段代码,因为当方向改变时,我的根视图中出现了奇怪的问题......我你看到只是调用了应该被调用但似乎确实是的方法。 ...这很好用,没有错误

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
        NSLog(@"landscape left");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
        NSLog(@"landscape right");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
        //do something or rather
        [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
        NSLog(@"portrait");
    }
}

Here is a snippet of code I hade to write because I was getting wierd issues coming back into my root view when the orientation was changed .... I you see just call out the method that should have been called but did seem to be .... this works great no errors

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
        NSLog(@"landscape left");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
        //do something or rather
        [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
        NSLog(@"landscape right");
    }
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
        //do something or rather
        [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
        NSLog(@"portrait");
    }
}
猫七 2024-09-03 07:28:42

UIInterfaceOrientation 现已弃用,UIDeviceOrientation 包括 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown,因此不能依赖为您提供界面方向。

解决方案虽然很简单

if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
    // Landscape
} else {
    // Portrait
}

UIInterfaceOrientation is now deprecated, and UIDeviceOrientation includes UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown so can't be relied on to give you the interface orientation.

The solution is pretty simple though

if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
    // Landscape
} else {
    // Portrait
}
小霸王臭丫头 2024-09-03 07:28:42

随着 [UIApplication statusBarOrientation] 被弃用,您现在应该使用:

UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];

UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;

With [UIApplication statusBarOrientation] being deprecated you should now use:

UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];

UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文