React Native integrated react-native-orientation (horizontal and vertical screen widget) sample code
1, the installation
#安装
npm install react-native-orientation --save
#链接所有依赖
react-native link react-native-orientation
2, the official API
#锁定为横屏
lockToPortrait()
#锁定为竖屏
lockToLandscape()
#锁定为左竖屏
lockToLandscapeLeft()
#锁定为右竖屏
lockToLandscapeRight()
#解除所有锁定
unlockAllOrientations()
#得到当前屏幕的信息
getOrientation((err, orientation) => {})
getSpecificOrientation((err, specificOrientation) => {})
3, add, remove monitor
Orientation.addOrientationListener((orientation)=>{});
Orientation.addSpecificOrientationListener((specificOrientation)=>{});
removeOrientationListener((orientation)=> {});
removeSpecificOrientationListener((specificOrientation)=> {});
4, sample code
import React, {Component} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
import Orientation from 'react-native-orientation';
export default class App extends Component<Props> {
constructor() {
super(...arguments);
this.state = {
isBool: Orientation.getInitialOrientation() === 'PORTRAIT',
summary: ''
};
this._handleOnPress = this._handleOnPress.bind(this);
}
componentDidMount() {
Orientation.lockToPortrait();
}
_handleOnPress = () => {
const {isBool} = this.state;
if (isBool) {
this.setState({isBool: false, summary: '本来是竖屏、现在锁定为横屏了....'});
Orientation.lockToLandscape();
} else {
this.setState({isBool: true, summary: '本来是横屏、现在锁定为竖屏了....'});
Orientation.lockToPortrait();
}
};
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>方向:{this.state.isBool ? '竖屏' : '横屏'}</Text>
<Text style={styles.welcome}>描述:{this.state.summary}</Text>
<Button title={'切换屏幕方向'}
onPress={() => this._handleOnPress()}/>
</View>
);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});
5, showing the effect of
1): Default
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190129155124564.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4NzczMA==,size_16,color_FFFFFF,t_70)
2): switching to landscape portrait
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190129155241140.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4NzczMA==,size_16,color_FFFFFF,t_70)
3): horizontal screen switches to portrait
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190129155307223.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4NzczMA==,size_16,color_FFFFFF,t_70)
6, packaged compilation solve unusual problems, Execution failed for task ': react-native-orientation: verifyReleaseResources'.
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190117095821368.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4NzczMA==,size_16,color_FFFFFF,t_70)
1): Modify node_modules \ react-native-orientation \ android \ build.gradle file dependencies compile is under implementation.
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190117100507790.png)
2): Modify node_modules \ react-native-orientation \ android \ build.gradle file, consistent with the android \ build.gradle file version.
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190117100955589.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE4NzczMA==,size_16,color_FFFFFF,t_70)
3): can be repackaged.
![Here Insert Picture Description](https://img-blog.csdnimg.cn/2019011710133783.png)