首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDevice方向

UIDevice方向
EN

Stack Overflow用户
提问于 2009-05-30 16:43:06
回答 8查看 45.4K关注 0票数 12

我在一个方法中有以下代码。当我在模拟器中运行这段代码时,调试器会跳过代码?我遗漏了什么?

代码语言:javascript
复制
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} else {

}
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2009-05-30 18:56:29

更新2

这应该无关紧要,但请尝试打开方向通知:

代码语言:javascript
复制
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


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

更新

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

尝试删除或语句,只测试单一方向。看看能不能修好它。也许有一个支架问题或一些愚蠢的事情。

我在生产代码中使用了以下测试,因此您的技术应该可以工作:

代码语言:javascript
复制
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {


}

原始答案

您必须在if块中实际放入语句才能使其进入。

调试器足够智能,可以跳过空块。

票数 18
EN

Stack Overflow用户

发布于 2011-06-14 21:23:06

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

代码语言:javascript
复制
 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

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

       //Portrait orientation

}

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

    //Landscape orientation

}

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

票数 54
EN

Stack Overflow用户

发布于 2009-12-01 14:49:55

请注意,这里有一个宏UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait,因此您可以这样做,而不是分别与LandscapeLeft和LandscapeRight进行比较:

代码语言:javascript
复制
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/930075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档