如果没有第三方库,我们可以使用此未记录的功能检测方向更改,如下所示:
import { DeviceEventEmitter } from 'react-native'
function handleOrientationDidChange(data) {
console.log('orientation changed, data:', data)
}
DeviceEventEmitter.addListener('namedOrientationDidChange', handleOrientationDidChange);
这为我们提供了如下数据:
{ rotationDegrees: -90, isLandscape: true, name: "landscape-primary" }
注意:我仅在Android上对此进行了测试。很高兴知道它是否也适用于iOS。
然而,这仅适用于 ON CHANGE 。有没有办法在启动时获取此信息?
答案 0 :(得分:1)
你试过this library!
以下是repo的示例用法:
componentWillMount() {
// The getOrientation method is async. It happens sometimes that
// you need the orientation at the moment the JS runtime starts running on device.
// `getInitialOrientation` returns directly because its a constant set at the
// beginning of the JS runtime.
const initial = Orientation.getInitialOrientation();
if (initial === 'PORTRAIT') {
// do something
} else {
// do something else
}
}
快乐编码:)