【问题标题】:UIDevice OrientationUIDevice 方向
【发布时间】:2010-10-30 03:03:10
【问题描述】:

我在方法中有以下代码。当我在模拟器中运行它时,调试器会直接跳过代码??我错过了什么?

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

} else {

}

【问题讨论】:

  • 你的意思是它跳过了if和else,还是总是去else?
  • 是的 if 和 else 中有数据;是的,它跳过了整个事情。

标签: iphone cocoa-touch ios-simulator


【解决方案1】:

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

 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]; 结合 UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortait 是要走的路。
  • 太好了,谢谢!为什么 Apple 推荐容易出现问题的代码? developer.apple.com/library/ios/featuredarticles/…
【解决方案2】:

请注意,有一个宏 UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait,因此与其将其分别与 LandscapeLeft 和 LandscapeRight 进行比较,不如这样做:

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

【讨论】:

    【解决方案3】:

    更新 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 中有数据;是的,它跳过了整个事情。
    【解决方案4】:

    在不打开方向通知的情况下执行此操作的另一种方法是

    第 1 步: 将当前方向保存在局部变量 myCurrentOrientation 中并像这样分配它:

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

    第 2 步:使用myCurrentOrientation 进行检查

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

    【讨论】:

    • 好建议。但请注意,当用户在另一个视图中更改方向并返回此处时,不会调用此代码。通过以一种方便的方法添加此解决方法:myCurrentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    【解决方案5】:

    假设您在 Springboard 调整中并希望根据当前应用的方向显示某些内容,那么您可以使用它(仅限越狱):

    UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];
    

    【讨论】:

      【解决方案6】:

      嘿,你需要打电话给[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] 在获取值之前。查看此方法的文档。我花了一段时间才找到这个。

      【讨论】:

        【解决方案7】:

        我建议您使用我突出显示的代码而不是您的代码来保护某些行代码。

        -(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) 
        {
        }
        

        【讨论】:

          【解决方案8】:

          这是一种查找屏幕方向和真实中心的方法。我使用了 Tuszy 的方法,所以我可以正确设置 UIActivityIndi​​catorView。

          - (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);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-01-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多