关于iPhone的方向

时间:2010-09-30 10:36:24

标签: iphone cocoa-touch

如何获取iPhone的当前定位? 我浏览了这个网站,发现了以下两种方法。

  • [UIApplication sharedApplication] .statusBarOrientation;
  • [[UIDevice currentDevice] orientation];

哪一种是获得当前定位的正确方法? 我在模拟器4.1下尝试了两种方法,但这两种方法都存在一些问题。

2 个答案:

答案 0 :(得分:6)

注册您的班级以收听UIDeviceOrientationDidChangeNotification,然后相应地处理设备方向。

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];

并正确处理设备的方向:

- (void)deviceRotated: (id) sender{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationFaceUp ||
        orientation == UIDeviceOrientationFaceDown)
    {
        //Device rotated up/down
    }

    if (orientation == UIDeviceOrientationPortraitUpsideDown)
    {
    }
    else if (orientation == UIDeviceOrientationLandscapeLeft)
    {
    }
    else if (orientation == UIDeviceOrientationLandscapeRight)
    {
    }
}

答案 1 :(得分:3)

[[UIDevice currentDevice] orientation]获取设备的当前物理方向。 [UIApplication sharedApplication].statusBarOrientation获取UI的方向。如果应用程序曾向shouldAutorotateToInterfaceOrientation:方法返回NO,则这两个值将不相同。