我在 react-native 应用程序中使用纵向模式。但我想捕获屏幕的旋转事件。
有没有办法做到这一点?
谢谢...
最佳答案
这个功能可能对你有帮助
创建 useOrientation.js
文件
import {useEffect, useState} from 'react';
import {Dimensions} from 'react-native';
export function useOrientation(){
const [orientation, setOrientation] = useState("PORTRAIT");
useEffect(() => {
Dimensions.addEventListener('change', ({window:{width,height}})=>{
if (width<height) {
setOrientation("PORTRAIT")
} else {
setOrientation("LANDSCAPE")
}
})
}, []);
return orientation;
}
关于react-native - React Native,使用纵向模式检测屏幕旋转变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472029/