目前代码
import Orientation from 'react-native-orientation';
<TouchableOpacity
onPress={() => {
const initial = Orientation.getInitialOrientation();
if (initial === 'PORTRAIT') {
// 当前竖屏
Orientation.lockToLandscapeLeft();
} else {
// 当前非竖屏
Orientation.lockToPortrait();
}
}}
style={{alignSelf: 'center', marginLeft: 10, marginRight: 10}}>
// ....
</TouchableOpacity>
但是这么写我发现他始终只能从竖屏
点击后到横屏
但是竖屏点击后, 再也没法从竖屏
回到横屏
。
请问这是为什么呢?
环境:
- 雷电模拟器
建议使用
react-native-orientation-locker
这个库react-native-orientation
bug 比较多getInitialOrientation
是在开头用的,并不是实时的你打印一下
initial
的值 就知道了通过
这个监听获取确切的是否横屏