简体   繁体   中英

Device Orientation on React Native

Is it possible in React Native to get the device orientation in degrees? I know that there are a few modules on npm, usually they offer return just a string whether the device is portrait or landscape.

I wanna try to animate a Wind Rose, and I would need more precisely orientation than just strings. In the browser, I could use the https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation

I'm wondering if I'd have to go for a WebView in this case or if I could accomplish without

You can use this which ships with RN. I tested on android:

 DeviceEventEmitter.addListener('namedOrientationDidChange', data => console.log('orientation changed, data:', data));

This only fires on change though. On startup, I don't know how to get the orientation.

The data is an object of this shape:

{ rotationDegrees: -90, isLandscape: true, name: "landscape-primary" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM