react-native ActivityIndicator实现loading框

ActivityIndicator组件,显示一个圆形的 loading 提示符号。

基本属性

属性 说明
animating 是否要显示指示器动画,true表示显示,false隐藏
size 指示器的大小,small表示尺寸小,large尺寸大
color 滚轮的前景颜色
hidesWhenStopped 在animating为 false 的时候,是否要隐藏指示器

组件介绍完,下面实现功能:loading框的实现可以结合modal组件,实现一种加载中有遮罩层且不可操作的效果,上代码:

import React, {Component} from 'react';
import {
    StyleSheet,
    View,
    Text,
    ActivityIndicator,
    TouchableOpacity,
    Modal
} from 'react-native';
import {connect} from 'react-redux';

class ActivityIndicatorDemo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            animating: false,
            modalVisible: false
        }
    }
    componentDidMount(){
        this.setState({
            animating: true,
            modalVisible: true
        })
        this._timer = setTimeout(() => {
            this.setState({
                animating: false,
                modalVisible: false
            });
        }, 3000);
    }

    componentWillUnmount(){
        this._timer && clearTimeout(this._timer);
    }

    render() {
        return(
            <View style={styles.container}>
                <Modal 
                    animationType={'fade'}
                    visible={this.state.modalVisible}
                    transparent={true}
                    onRequestClose={() => {
                        // alert("Modal has been closed.");
                    }}
                >
                    <ActivityIndicator 
                        style={styles.loadingStyle}
                        size="large" // 指示器的大小
                        color='#1a94fc' // 滚轮的前景颜色
                        animating={this.state.animating} // 是否要显示指示器动画 true表示显示 false隐藏
                        hidesWhenStopped={true} // 在animating为false的时候,是否要隐藏指示器
                    />
                </Modal>
            </View>
        )
    }

}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#FFF',
    },
    loadingStyle:{
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'rgba(0,0,0,0.1)'
    }
});

const mapStateToProps = state => ({
    nav: state.nav,
})

const mapDispatchToProps = (dispatch) => {
    return {
        dispatch
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(ActivityIndicatorDemo)

效果图:

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,455评论 25 708
  • 母亲说:只要有芝麻吃,生活总不会太差。 前几日拿出来要炒的芝麻,放在桌子上,想着要抽时间捡干净。只是第二天还没来得...
    Someoner阅读 842评论 3 3
  • 今天,孩子爸爸说打掉素素,给我一万块钱,我说,钱先给我,他说你去医院打了再给钱.我苦涩的笑,趴在桌上痛苦一场,然后...
    杨俪颖阅读 213评论 0 0
  • 今天我们来猜个谜语: 红果子,麻点子,咬一口,甜丝丝。 猜出来了嘛?没错,就是草莓。 ...
    魏大勋的港岛妹妹阅读 650评论 6 11
  • 今天,我读了《钢铁是怎样炼成的》这本书,书中的主人公——保尔使我油然而生敬意。 保尔被老师神甫赶出学校后,...
    倪俊芸阅读 251评论 0 1