About the orientation of iPhone

前端 未结 2 1859
野性不改
野性不改 2021-01-16 01:42

How to get the current orientation of iPhone? I surfed this site, and found two methods as followings.

  • [UIApplication sharedApplication].statusBarOrientation;<
2条回答
  •  悲哀的现实
    2021-01-16 02:36

    Register your class to listen to UIDeviceOrientationDidChangeNotification then handle the device orientation accordingly.

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

    and handle the device's orientation properly:

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

提交回复
热议问题