ReactNative顶部进度条

经常看到一些app在页面加载的时候顶部都有个进度条  他们说那个页面是webView  我觉得不是webView 也可以用一用 所以就试着写了写 自己撸了一个出来

直接上代码 主要还是为了自己以后用的时候少写点 真累啊

/**
 * Created by wangqiang on 2017/7/25.
 */
import React from 'react';
import {View,StyleSheet,Image,ActivityIndicator,Text,StatusBar,Dimensions} from 'react-native';
import ProgressBar from './ProgressBar';
export default class ProgressDemo extends React.PureComponent{

    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            progress:0,
            loading:true,
            progressBar:true
        };
      }

    componentWillMount() {
        StatusBar.setNetworkActivityIndicatorVisible(true);
        StatusBar.setBarStyle('light-content')
        this.timer = setInterval(()=>{
            if(this.state.progress ==1){
                clearInterval(this.timer);
                StatusBar.setNetworkActivityIndicatorVisible(false);
            }else {
                let random = Math.random() * 0.2;
                let progress = this.state.progress + random;
                if(progress>1){
                    progress = 1;
                }
                this.setState({progress});
            }
        },200);
    }

    componentDidMount() {
        this.timers = setTimeout(()=>{
            this.setState({
                loading:false,
                progressBar:false
            })
        },3000)
    }

    render(){

          if(this.state.loading){
              return (
                  <View style={styles.container}>
                      <View style={styles.titleBar}>
                          <Text style={styles.title}>赵丽颖</Text>
                      </View>
                      {this.state.loading
                          ?  <ProgressBar
                              color='#ee82ee'
                              style={styles.progress}
                              progress={this.state.progress}
                              width={400}
                          />
                          :<View style={{height:0}}/>
                      }
                  </View>
              )
          }else {
              return(
                  <View style={styles.container}>
                      <View style={styles.titleBar}>
                          <Text style={styles.title}>赵丽颖</Text>
                      </View>
                      <View style={styles.imgView}>
                          <Image
                              resizeMode="cover"
                              source={{uri:'http://www.sinaimg.cn/dy/slidenews/3_img/2017_11/85178_416432_295268.jpg'}}
                              style={styles.img}
                          />
                      </View>
                  </View>
              )
          }


    }
}

const styles = StyleSheet.create({
    progress:{
        backgroundColor: 'transparent',
    },
    container:{
        flex:1,
        backgroundColor:'#fff',
        // justifyContent:'center',
        // alignItems:'center'
    },
    img:{
        width:Dimensions.get('window').width,
        height:Dimensions.get('window').height
    },
    titleBar:{
        height:64,
        backgroundColor:'#ffc0cb',
        justifyContent:'center',
        alignItems:'center'
    },
    imgView:{
        justifyContent:'center',
        alignItems:'center',
    },
    title:{
       color:'#fff',
        fontSize:16,
        marginTop:20
    }
});
不会上传动图 就随便截图了  自己下载下来看效果 还是不错的 首先是开始加载了 然后加载完毕 进度条消失 图片就出来了





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值