Reactjs 在不更改方向的情况下响应本机Expo方向侦听器

Reactjs 在不更改方向的情况下响应本机Expo方向侦听器,reactjs,react-native,expo,screen-orientation,Reactjs,React Native,Expo,Screen Orientation,我正在尝试在我的React Native应用程序上检测当前方向,而无需更改方向。我制作了一个摄像头应用程序,我只想在方向改变时翻转图标。现在我不能使用ScreenOrientation.addOrientionChangeListener(listener)而不改变实际方向。有什么办法可以解决这个问题吗?如果我理解正确,那么需要获得设备的方向,即使实际方向没有改变,因为它被锁定了 您可以使用模块获取设备旋转() DeviceMotion.addListener(({rotation})=>{ 常

我正在尝试在我的React Native应用程序上检测当前方向,而无需更改方向。我制作了一个摄像头应用程序,我只想在方向改变时翻转图标。现在我不能使用
ScreenOrientation.addOrientionChangeListener(listener)
而不改变实际方向。有什么办法可以解决这个问题吗?

如果我理解正确,那么需要获得设备的方向,即使实际方向没有改变,因为它被锁定了

您可以使用模块获取设备旋转()

DeviceMotion.addListener(({rotation})=>{
常数α=数学绝对值(旋转角度α);
这是我的国家({
方向:alpha>3 | |(alpha>0&&alpha<0.5)?“横向”:protrait
});
});
现场演示:


旧答案


如果我理解正确,您希望在应用程序加载时获得当前方向,而用户不更改方向

如果是这样,您可以从中获取它,它将返回一个带有

实例:

我发现,类似于使用DeviceMotion,但使用gamma和beta(x和y轴)而不是alpha(z轴)来计算设备的旋转。在我的例子中,我用这种技术获得了更令人满意的结果

isPortrait(gamma: number, beta: number) {
 const ABSOLUTE_GAMMA = Math.abs(gamma);
 const ABSOLUTE_BETA = Math.abs(beta);
 const isGammaNegative = Math.sign(gamma) === -1;

 if (ABSOLUTE_GAMMA <= 0.04 && ABSOLUTE_BETA <= 0.24) {
  //Portrait mode, on a flat surface.
  return true;
 } else 
 if (
   (ABSOLUTE_GAMMA <= 1.0 || ABSOLUTE_GAMMA >= 2.3) &&
   ABSOLUTE_BETA >= 0.5
 ) {
   //General Portrait mode, accounting for forward and back tilt on the top of the phone.
   return true;
 } else {
   if (isGammaNegative) {
     //Landscape mode with the top of the phone to the left.
     return false;
   } else {
     //Landscape mode with the top of the phone to the right.
     return false;
   }
 }
}

为了让这一切顺利进行,我需要:

  • 向componentDidMount添加代码:
异步组件didmount(){ if(等待DeviceMotion.isAvailableAsync()){ DeviceMotion.addListener(this.calculateRotation) } // ...
}不幸的是,这没有帮助。我想检测手机何时处于横向位置。但我不想在这种情况下改变方向。当实际方向不变时,不会触发GetOrientionAsync():(
无需更改方向
无需更改实际方向
,因此我认为您希望在方向未更改时进行检测,因此我不确定是什么情况。您能否澄清?是的,我正在尝试锁定屏幕,但仍然能够检测到更改。我尝试了您的代码,但它一直在说端口rait:(非常感谢!它很有效。我最终使用了{orientation}而不是{rotation}。同样有效并且更加一致!这导致了一个错误:DeviceMotion.addListener(This.calculateRotation)如果关闭组件窗口,将出现无法设置未安装组件状态的错误。因为关闭时未运行this.calculateRotation。
isPortrait(gamma: number, beta: number) {
 const ABSOLUTE_GAMMA = Math.abs(gamma);
 const ABSOLUTE_BETA = Math.abs(beta);
 const isGammaNegative = Math.sign(gamma) === -1;

 if (ABSOLUTE_GAMMA <= 0.04 && ABSOLUTE_BETA <= 0.24) {
  //Portrait mode, on a flat surface.
  return true;
 } else 
 if (
   (ABSOLUTE_GAMMA <= 1.0 || ABSOLUTE_GAMMA >= 2.3) &&
   ABSOLUTE_BETA >= 0.5
 ) {
   //General Portrait mode, accounting for forward and back tilt on the top of the phone.
   return true;
 } else {
   if (isGammaNegative) {
     //Landscape mode with the top of the phone to the left.
     return false;
   } else {
     //Landscape mode with the top of the phone to the right.
     return false;
   }
 }
}
const sensorAvailable = await DeviceMotion.isAvailableAsync();
if (sensorAvailable) {
 DeviceMotion.addListener(({ rotation }) => {
  const { gamma, beta } = rotation;
  this.setState({
    orientation: this.isPortrait(gamma, beta) ? 'portrait' : 'landscape'
   });
  });
 }