关于iPhone的方向

Aec*_*Liu 1 iphone cocoa-touch

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

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

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

Hoa*_*ham 6

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

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
Run Code Online (Sandbox Code Playgroud)

并正确处理设备的方向:

- (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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)