React-Native ProgressViewIOS与圆形进度条react-native-percentage-circle

1:ProgressViewIOS:为react-native组件自带的长条形进度条。
主要属性:

`progress`:当前的进度,实现进度变化

`trackTintColor`:进度条的底色,默认为灰色

`progressTintColor`:进度变化的颜色,默认蓝色

`progressViewStyle`:enum('default', 'bar')  default显示底部颜色,bar不显示底部颜色

实现一个ProgressViewIOS进度条:

<ProgressViewIOS style={{marginTop: 20, width: 300}} progress={(this.state.progress2)}
                                 progressTintColor={'#11fffa'}
                                 trackTintColor={'#ff0000'}/>

2:如何像安卓的ProgressBarAndroid实现圆形进度条嘞?查了下,发现有大神已经搞定了,使用react-native-percentage-circle组件,github连接:https://github.com/JackPu/react-native-percentage-circle

第一步:cd到项目根目录执行命令
`npm i react-native-percentage-circle --save`

第二步:在js文件中
`import PercentageCircle from 'react-native-percentage-circle';`
导入组件

第三步:开始使用啦,就这样:

//radius:为半径
//percent:进度值0-100
//color:进度颜色
<PercentageCircle style={{marginTop: 60}}
                                  radius={45}
                                  percent={this.state.progress3}
                                  color={"#f498db"}>
                </PercentageCircle>

执行效果如下:

Untitled11.gif
有木有发现问题:

1:PercentageCirclestyle属性无效
2:为啥进度开始的时候从左下方开始读条,而且显示进度条变没了???

于是进到react-native-percentage-circle组件,准备查找问题,解决后的代码:

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
} from 'react-native';

const styles = StyleSheet.create({
  circle: {
    overflow: 'hidden',
    position: 'relative',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#e3e3e3',
  },
  leftWrap: {
    overflow: 'hidden',
    position: 'absolute',
    top: 0,
  },
  rightWrap: {
    position: 'absolute',

  },

  loader: {
    position: 'absolute',
    left: 0,
    top: 0,
    borderRadius: 1000,

  },

  innerCircle: {
    overflow: 'hidden',
    position: 'relative',
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    fontSize: 11,
    color: '#888',
  },
});

class PercentageCircle extends Component {
  propTypes: {
    color: React.PropTypes.string,
    bgcolor: React.PropTypes.string,
    innerColor: React.PropTypes.string,
    radius: React.PropTypes.number,
    percent: React.PropTypes.number,
    borderWidth: React.Proptypes.number,
    textStyle: React.Proptypes.array,
    disabled: React.PropTypes.bool,
//在这里增加一个属性,用来提供style
      pstyle: React.PropTypes.array,
  }

  constructor(props) {
    super(props);

    let percent = this.props.percent;
    let leftTransformerDegree = '0deg';
    let rightTransformerDegree = '0deg';
    if (percent >= 50) {
      rightTransformerDegree = '180deg';
      leftTransformerDegree = (percent - 50) * 3.6 + 'deg';
    } else {
      rightTransformerDegree = percent * 3.6 + 'deg';
    }

    this.state = {
      percent: this.props.percent,
      borderWidth: this.props.borderWidth < 2 || !this.props.borderWidth ? 2 : this.props.borderWidth,
      leftTransformerDegree: leftTransformerDegree,
      rightTransformerDegree: rightTransformerDegree,
      textStyle: this.props.textStyle ? this.props.textStyle : null
    };
  }

  componentWillReceiveProps(nextProps) {
//这里改成和constructor一样的判断
    let percent = nextProps.percent;
      let leftTransformerDegree = '0deg';
      let rightTransformerDegree = '0deg';
      if (percent >= 50) {
          rightTransformerDegree = '180deg';
          leftTransformerDegree = (percent - 50) * 3.6 + 'deg';
      } else {
          rightTransformerDegree = percent * 3.6 + 'deg';
      }
    this.setState({
      percent: this.props.percent,
      borderWidth: this.props.borderWidth < 2 || !this.props.borderWidth ? 2 : this.props.borderWidth,
      leftTransformerDegree: leftTransformerDegree,
      rightTransformerDegree: rightTransformerDegree
    });
  }

  render() {
    if (this.props.disabled) {
      return (
//这里增加this.props.pstyle属性
        <View style={[styles.circle, this.props.pstyle, {
          width:this.props.radius*2,
          height: this.props.radius*2,
          borderRadius:this.props.radius
        }]}>
          <Text style={styles.text}>{this.props.disabledText}</Text>
        </View>
      );
    }
    return (
//这里增加this.props.pstyle属性
      <View style={[styles.circle, this.props.pstyle, {
        width:this.props.radius*2,
        height: this.props.radius*2,
        borderRadius:this.props.radius,
        backgroundColor: this.props.bgcolor
      }]}>
        <View style={[styles.leftWrap,{
          width: this.props.radius,
          height: this.props.radius * 2,
          left:0,
        }]}>
          <View style={[styles.loader,{
            left: this.props.radius,
            width:this.props.radius,
            height: this.props.radius*2,
            borderTopLeftRadius:0,
            borderBottomLeftRadius:0,
            backgroundColor:this.props.color,
            transform:[{translateX:-this.props.radius/2},{rotate:this.state.leftTransformerDegree},{translateX:this.props.radius/2}],  
          }]}></View>
        </View>
        <View style={[styles.leftWrap,{
          left:this.props.radius,
          width: this.props.radius,
          height: this.props.radius * 2,
        }]}>
          <View style={[styles.loader,{
            left:-this.props.radius,
            width:this.props.radius,
            height: this.props.radius*2,
            borderTopRightRadius:0,
            borderBottomRightRadius:0,
            backgroundColor: this.props.color,
//这里的判断取消
            transform:[{translateX:this.props.radius/2},{rotate:this.state.rightTransformerDegree},{translateX:-this.props.radius/2}],  
          }]}></View>
        </View>
        <View style={[styles.innerCircle,{
              width:(this.props.radius - this.state.borderWidth)*2, 
              height:(this.props.radius - this.state.borderWidth)*2,
              borderRadius:this.props.radius - this.state.borderWidth,
              backgroundColor: this.props.innerColor,
            }]}>
          {this.props.children ? this.props.children :
            <Text style={[styles.text, this.state.textStyle]}>{this.props.percent}%</Text>}
        </View>

      </View>
    );
  }
}

// set some attributes default value
PercentageCircle.defaultProps = {
  bgcolor: '#e3e3e3',
  innerColor: '#fff'
};

module.exports = PercentageCircle;

</pre>

#####然后刷新下,效果如下:

![Untitled12.gif](http://upload-images.jianshu.io/upload_images/5708383-409a4aab6b81ab5f.gif?imageMogr2/auto-orient/strip)

嘿嘿嘿,搞定了,以下是我的js代码
<pre>
import React, { Component } from 'react';
import {
    AppRegistry,
    View,
    ProgressViewIOS,
    Text
} from 'react-native';

import PercentageCircle from 'react-native-percentage-circle';

export default class RNProgressView extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            progress: 0,
            progress2: 0,
            progress3: 10,
        }
    }
    componentDidMount() {
        this.timer1 = setInterval(
            ()=>{
                this.updateProgress();
            },
            1000,
        );
    }
    updateProgress = ()=>{
        console.log('updateProgress111111');
        let progress = this.state.progress + 0.01;
        let progress2 = this.state.progress2 + 0.1;
        let progress3 = this.state.progress3 + 10;
        if (progress > 1) {
            progress = 0
        }
        if (progress2 > 1){
            progress2 = 0
        }
        if (progress3 > 100){
            progress3 = 0
        }


        this.setState({
            progress: progress,
            progress2: progress2,
            progress3: progress3
        });


    };
    getProgress = (offset)=>{
        console.log('getProgress2222222');
        let progress = this.state.progress + offset;
        return progress
    };
    render() {
        return(
            <View style={{flex: 1, backgroundColor: '#aaaaaa', alignItems:'center', justifyContent:'center'}}>
                <Text> 显示progressView </Text>
                <ProgressViewIOS style={{marginTop: 20, width: 300}} progress={this.getProgress(0)}/>
                <ProgressViewIOS style={{marginTop: 20, width: 300}} progress={this.getProgress(0.06)}
                                 progressTintColor={'#11fffa'}
                                 trackTintColor={'#ff0000'}/>
                <ProgressViewIOS style={{marginTop: 20, width: 300, transform:[{scaleY:2.5}]}}
                                 progress={(this.state.progress2)}
                                 progressTintColor={'#faff00'}
                                 progressViewStyle={'bar'}/>

                <PercentageCircle pstyle={{marginTop: 60}}
                                  radius={45}
                                  percent={this.state.progress3}
                                  color={"#f498db"}>

                </PercentageCircle>
            </View>
        )
    }
}
AppRegistry.registerComponent('RNProgressView', ()=>RNProgressView);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容