reactnative loading

1.组件 LoadingUnitl.js

let LoadingUtil = {
    showLoading(timeOut = 10000) {
        this.startShowLoading();
        this.timerLoading = setTimeout(() => {
            this.dismissLoading();
        }, timeOut);
    },
    /**
     * 延迟500ms显示loading动画
     * @param delay 延迟ms数
     */
    showLoadingDelay(delay = 500) {
        this.timerLoading && clearTimeout(this.timerLoading);
        this.dissTimer && clearTimeout(this.dissTimer);
        this.timerLoading = setTimeout(() => {
            this.startShowLoading();
        }, delay);
        this.dissTimer = setTimeout(() => {
            this.dismissLoading();
        }, 60000);
    },
    startShowLoading() {
        global.mLoadingComponentRef && global.mLoadingComponentRef.showLoading();
    },
    dismissLoading() {
        global.mLoadingComponentRef && global.mLoadingComponentRef.dismissLoading();
        this.timerLoading && clearTimeout(this.timerLoading);
        this.dissTimer && clearTimeout(this.dissTimer);
    },
};

export default LoadingUtil;

loadingView.js

import React from "react";
import {ActivityIndicator, Dimensions, StyleSheet, Text, View} from "react-native";
import {sizePx} from "../utils/ScreenUtil";

const {width, height} = Dimensions.get("screen");

export default class LoadingView extends React.Component {
    constructor(props) {
        super(props);
        this.minShowingTime = 200;
        this.state = {
            // fadeAnim: new Animated.Value(0),
            isLoading: false,
            setIsLoading: (isLoading) => {
                if (isLoading !== this.state.isLoading) {
                    let curTimeLong = new Date().getTime();
                    if (isLoading) {
                        this.startTime = curTimeLong;
                        this.setState({
                            isLoading
                        });
                    } else {
                        let hasShowingTimeLong = curTimeLong - this.startTime;
                        if (hasShowingTimeLong < this.minShowingTime) {
                            setTimeout(() => {
                                this.setState({
                                    isLoading
                                });
                            }, this.minShowingTime - hasShowingTimeLong);

                        } else {
                            this.setState({
                                isLoading
                            });
                        }
                    }

                }
            },
        };
    }

    showLoading = () => {
        this.state.setIsLoading(true);
    };
    dismissLoading = () => {
        this.state.setIsLoading(false);
    };

    _doJump = () => {
        // this.state.fadeAnim.setValue(0);
        // this.animate = Animated.timing(
        //     this.state.fadeAnim,
        //     {
        //         toValue: 1,
        //         duration: 2000,
        //     }
        // ).start(this._doJump);
    };

    componentWillMount() {
        this._doJump();
    }

    componentWillUnmount() {
        if (this.animate) {
            this.animate.stop();
        }
    }

    render() {
        if (!this.state.isLoading) {
            return null;
        }
        return (
            <View style={styles.containerStyle}>
                <View style={[styles.loading]}>
                    {/*<Animated.Image source={icon.logo}*/}
                                    {/*style={[styles.logoStyle,*/}
                                        {/*{*/}
                                            {/*transform: [*/}
                                                {/*{*/}
                                                    {/*rotateY: this.state.fadeAnim.interpolate({*/}
                                                        {/*inputRange: [0, 1],*/}
                                                        {/*outputRange: ['0deg', '360deg']*/}
                                                    {/*})*/}
                                                {/*}]*/}
                                        {/*}]}/>*/}
                    <ActivityIndicator color="#3478F6" size={"large"}/>
                    {/*<Text style={styles.loadingTitle}>加载中...</Text>*/}
                </View>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    containerStyle: {
        width: width,
        height: height,
        position: 'absolute',
        top: 0,
        left: 0,
        // backgroundColor: 'rgba(0,0,0,0)',
        flex: 1,
    },
    loading: {
        // backgroundColor: '#10101099',
        height: sizePx(200),
        width: sizePx(200),
        borderRadius: sizePx(8),
        justifyContent: 'center',
        alignItems: 'center',
        position: 'absolute',
        top: (height - sizePx(200)) / 2,
        left: (width - sizePx(200)) / 2,
    },

    logoStyle: {
        width: sizePx(63),
        height: sizePx(75),
        resizeMode: 'contain',
    },

    loadingTitle: {
        marginTop: sizePx(20),
        fontSize: sizePx(28),
        color: '#3478F6'
    }
});

2.使用

放在App.js 全局使用 也可某个页面单独添加

<LoadingView
                        ref={(ref) => {
                            global.mLoadingComponentRef = ref;
                        }}
                    />

LoadingUtil.showLoadingDelay(500);
LoadingUtil.dismissLoading();
LoadingUtil.startShowLoading();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 基础开发技巧 1.1 AppRegistry AppRegistry模块则是用来告知React Native哪...
    Kevin_Junbaozi阅读 1,502评论 0 2
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,082评论 0 29
  • 我以前是不大喜欢奶奶(爸爸的妈妈),那时候老人儿女多,孙子辈的也自然多。老人始终是不喜欢我和弟弟。转眼,阴阳相隔已...
    蝉翼呵呵阅读 441评论 0 1
  • 王福利 二姐现在五十一岁,有一女一儿,儿己成家,女考上大学己参加工作。姐夫人灵手勤,日子过的红红火火,有楼有车。二...
    惜王福利阅读 246评论 0 1
  • 所有命运赠送的礼物,都暗中标着价格。 这句牛掰的话是茨威格说的,在他那本《命丧断头台的法国王后——玛丽·安托瓦内特...
    兰安晨Rita阅读 442评论 1 1