首页 > 解决方案 > React Native - 方向锁定;我还能得到传感器的方向吗?

问题描述

我正在开发一个旨在以纵向模式运行的 React Native 应用程序。方向通过清单锁定:

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait">

我需要添加拍摄我使用过的照片的功能react-native-camera

问题是,我无法在拍照时改变方向,因为它被锁定为纵向。

有没有办法允许在特定视图(即相机容器)中改变方向,以便相机可以检测到正确的方向?

我在想也许以下是可能的:

  1. 直接从传感器请求方向
  2. 没有锁定方向的新活动

标签: androidreact-native

解决方案


我遇到过同样的问题。我使用了 React-Native-Orientation:https ://github.com/yamill/react-native-orientation 。

您不应该在清单中锁定方向,而是可以使用此库将每个屏幕锁定为纵向模式。采用lockToPortrait()

对于您需要旋转的屏幕,只需使用 unlockAllOrientations 或lockToLandscape()

API 很棒,但问题是你需要为每个屏幕都做这件事。如果你有很多屏幕,这似乎有点矫枉过正。但效果很好

附加提示

在每个屏幕上都这样做会很忙而且很难管理。我所做的是将我的导航功能模块化为一个类。一世

import Orientation from 'react-native-orientation';

export const navigateToMainScreen=()=>{
  determineOrientation("Main");
  this.props.navigation.navigate("Main");
}

const determineOrientation=(screenName)=>{
  // ur logic
  if(true){
      Orientation.lockToPortrait();
   }
  else{
   Orientation.lockToLandscape();
   }
}

推荐阅读