Android/iOS手机朝向获取

对于iOS手机:

苹果公司给出了一个枚举,如下:

 typedef NS_ENUM (NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // 竖向,home键向下
    UIDeviceOrientationPortraitUpsideDown,  //  竖向,home键向上
    UIDeviceOrientationLandscapeLeft,       // 横向,home键向右
    UIDeviceOrientationLandscapeRight,      //  横向,home键向左
    UIDeviceOrientationFaceUp,              // 屏幕平放,向上
    UIDeviceOrientationFaceDown             // 屏幕平放,向下
}
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {

    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,

    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,

    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,

    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,

    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

}

2、对于获取手机屏幕
(1)

[[ UIDevice currentDevice ] beginGeneratingDeviceOrientationNotifications ];
    dispatch_async ( dispatch_get_main_queue (), ^{
        NSLog ( @"=========%zd" ,[[ UIDevice currentDevice ] orientation ]);
    });
    [[ UIDevice currentDevice ] endGeneratingDeviceOrientationNotifications ];

(2)

UIInterfaceOrientation orientation = [ UIApplication sharedApplication ]. statusBarOrientation ;

3、对于当前手机是不是横屏或者竖屏的判断
(1)判断是否是竖屏

static inline BOOL UIDeviceOrientationIsPortrait( UIDeviceOrientation orientation) {
    return ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown );
}

(2)判断是否是横屏

static inline BOOL UIDeviceOrientationIsLandscape( UIDeviceOrientation orientation) {
    return ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight );
}
对于Android手机:
public class SimpleOrientationActivity extends Activity {
    OrientationEventListener mOrientationListener;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        mOrientationListener = new OrientationEventListener(this,
            SensorManager.SENSOR_DELAY_NORMAL) {
 
            @Override
            public void onOrientationChanged(int orientation) {
                Log.v(DEBUG_TAG,
                    "Orientation changed to " + orientation);
            }
        };
 
       if (mOrientationListener.canDetectOrientation()) {
           Log.v(DEBUG_TAG, "Can detect orientation");
           mOrientationListener.enable();
       } else {
           Log.v(DEBUG_TAG, "Cannot detect orientation");
           mOrientationListener.disable();
       }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOrientationListener.disable();
    }
}

判断手机方向的具体判断代码如下:

if (OrientationEventListener.ORIENTATION_UNKNOWN == orientation) { 
    return; //手机平放时,检测不到有效的角度 
}

// 我们只关心 0, 90, 180 ,270 测试发现,很多设备返回的数据是很混乱的,但是这四个度数比较稳定
if((0 == orientation % 90) && (orientation < 360) && (orientation >= 0)) {
    //只检测是否有四个角度的改变
    if (orientation > 350 || orientation < 10) { //0度
        orientation = 0;
    } else if (orientation > 80 && orientation < 100) { //90度
        orientation = 90;
    } else if (orientation > 170 && orientation < 190) { //180度
        orientation = 180;
    } else if (orientation > 260 && orientation < 280) { //270度
        orientation = 270;
    } else {
        return;
    }
    Log.i("MyOrientationDetector ", "onOrientationChanged:" + orientation);
}
对于 Flutter:

使用 native_device_orientation 插件完成相同的检测功能。

参考链接