设备方向

12
我在一个方法中有以下代码。当我在模拟器中运行时,调试器会直接跳过这段代码? 我漏掉了什么吗?
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} else {

}

你的意思是它会跳过if和else,还是总是会进入else? - Joe Cannatti
是的,if和else中都有数据;而且它确实跳过了整个内容。 - Jordan
也许这个链接有帮助 https://dev59.com/Y3RB5IYBdhLWcg3wZmhz - beryllium
8个回答

54

确定界面方向的最佳方法是查看状态栏方向:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if(orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) {

       //Portrait orientation

}

if(orientation == UIInterfaceOrientationLandscapeRight ||
   orientation == UIInterfaceOrientationLandscapeLeft) {

    //Landscape orientation

}

UIDevice 类基于加速度计测量设备的方向,如果设备平放,则不会返回正确的方向。


UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 结合 UIDeviceOrientationIsLandscape 和 UIDeviceOrientationIsPortait 是解决问题的方法。 - Joe
太棒了,谢谢!为什么苹果推荐可能会出现问题的代码呢?https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html#//apple_ref/doc/uid/TP40007457-CH7-SW1 - brainray

23

请注意,有一个宏 UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait,因此您不必将设备方向单独与 LandscapeLeft 和 LandscapeRight 进行比较,只需像这样操作即可:

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}

18

更新2

虽然这可能无关紧要,但尝试打开方向通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


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

更新

我错了,我以为它是空的。

尝试删除or语句,只测试单个方向。看看是否可以修复问题。也许有括号问题或其他愚蠢的问题。

我已经在生产代码中使用以下测试,所以您的技术应该有效:

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {


}
你需要在 if 语句块中放置语句才能使其执行。调试器会跳过空的代码块。

是的,在if和else中都有数据;以及是的,它会跳过整个内容。 - Jordan

2

另一种不需要打开方向通知的方法是:

步骤1:将当前方向保存在本地变量myCurrentOrientation中,并像这样分配:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    myCurrentOrientation = toInterfaceOrientation;
}

步骤二:使用myCurrentOrientation进行检查

if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) {
    // landscape
}
else {
    // portrait.
}

好建议。但要注意,当用户在另一个视图中更改方向并返回此处时,此代码不会被调用。通过在方便的方法中添加以下内容来解决问题:myCurrentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; - FeltMarker

0
假设你正在编写一个Springboard插件,并且想要根据当前应用的方向显示某些内容,那么你可以使用以下代码(仅限越狱设备):
UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];

0

我建议您使用我突出显示的代码,而不是您自己的代码,以节省一些代码行。

-(void) viewDidLoad
{
    [super viewDidLoad];
    [self rotations];
}

-(void)rotations
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                         name:UIDeviceOrientationDidChangeNotification
                                         object:nil];
}

-(void) orientationChanged:(NSNotification *)notification
{
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    //USE THIS PART
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
    }
}

取代

if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait || 
   [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) 
{
}

0

这里有一种方法可以找到屏幕的方向和真正的中心。我使用了Tuszy的方法,以便正确设置UIActivityIndicatorView。

- (BOOL) isPortraitOrientation {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationPortrait ||
       orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return true;
    }
    if(orientation == UIInterfaceOrientationLandscapeRight ||
       orientation == UIInterfaceOrientationLandscapeLeft) {
        return false;
    }
    return false;
}

获取中心的方法是...

- (void) findMyUIViewCenter {
    CGPoint myCenter;
    if ([self isPortraitOrientation]) {
        myCenter = self.view.center;
    }
    else {
        myCenter = CGPointMake(self.view.frame.size.height / 2.0, self.view.frame.size.width / 2.0);
    }
    NSLog(@"true center -- x:%f y:%f )",myCenter.x,myCenter.y);
}

0

嘿,你需要在获取值之前调用[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]方法。查看一下这个方法的文档。我花了一段时间才找到这个问题。


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