ネイティブ反応ネイティブ配向(水平および垂直スクリーンウィジェット)サンプルコードを統合反応

ネイティブ反応ネイティブ配向(水平および垂直スクリーンウィジェット)サンプルコードを統合反応

1、インストール

#安装
npm install react-native-orientation --save
#链接所有依赖
react-native link react-native-orientation

2、公式のAPI

#锁定为横屏
lockToPortrait()

#锁定为竖屏
lockToLandscape()

#锁定为左竖屏
lockToLandscapeLeft()

#锁定为右竖屏
lockToLandscapeRight()

#解除所有锁定
unlockAllOrientations()

#得到当前屏幕的信息
getOrientation((err, orientation) => {})
getSpecificOrientation((err, specificOrientation) => {})

図3は、モニタを削除、追加します

/**
 * 添加监听
 *
 * orientation 将返回以下值之一:
 *
 * LANDSCAPE
 * PORTRAIT
 * PORTRAITUPSIDEDOWN
 * UNKNOWN
 */
 Orientation.addOrientationListener((orientation)=>{});
 /**
   * specificOrientation 将返回以下值之一:
   *
   * LANDSCAPE-LEFT
   * LANDSCAPE-RIGHT
   * PORTRAIT
   * PORTRAITUPSIDEDOWN
   * UNKNOWN
   */
 Orientation.addSpecificOrientationListener((specificOrientation)=>{});

 //移除监听
 removeOrientationListener((orientation)=> {};
 removeSpecificOrientationListener((specificOrientation)=> {};

4、サンプルコード

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',//当前是否是竖屏(true),横屏(false)
            summary: ''//描述
        };

        this._handleOnPress = this._handleOnPress.bind(this);
    }


    //生命周期函数、render渲染完成之后调用
    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の効果を示します

1):デフォルト

ここに画像を挿入説明

2)風景肖像画に切り替え

ここに画像を挿入説明

3):横画面をポートレートに切り替わり

ここに画像を挿入説明

図6は、コンパイルが異常な問題を解決するためのパッケージ、実行はタスクに失敗しました「:反応-ネイティブの向きを:verifyReleaseResources」。

ここに画像を挿入説明

1):反応-ネイティブの向きを\アンドロイドの\ build.gradleファイルの依存関係が実装中であるコンパイル\ node_modulesを変更します。

ここに画像を挿入説明

2):アンドロイド\ build.gradleファイルのバージョンと一致して反応し、ネイティブの向きを\アンドロイド\のbuild.gradleファイルは、\ node_modulesを変更します。

ここに画像を挿入説明

3):再パッケージ化することができます。

ここに画像を挿入説明

4):RNパッケージ化オフラインAPKの参照リンク:https://blog.csdn.net/weixin_44187730/article/details/86492907

おすすめ

転載: blog.csdn.net/weixin_44187730/article/details/86691850