React-Native ProgressViewIOS與圓形進度條react-native-percentage-circle

1:ProgressViewIOS:為react-native組件自帶的長條形進度條克握。
主要屬性:

`progress`:當前的進度,實現(xiàn)進度變化

`trackTintColor`:進度條的底色匣吊,默認為灰色

`progressTintColor`:進度變化的顏色巩梢,默認藍色

`progressViewStyle`:enum('default', 'bar')  default顯示底部顏色寇甸,bar不顯示底部顏色

實現(xiàn)一個ProgressViewIOS進度條:

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

2:如何像安卓的ProgressBarAndroid實現(xiàn)圓形進度條嘞橙喘?查了下挑庶,發(fā)現(xiàn)有大神已經(jīng)搞定了陆爽,使用react-native-percentage-circle組件什往,github連接:https://github.com/JackPu/react-native-percentage-circle

第一步:cd到項目根目錄執(zhí)行命令
`npm i react-native-percentage-circle --save`

第二步:在js文件中
`import PercentageCircle from 'react-native-percentage-circle';`
導(dǎo)入組件

第三步:開始使用啦茶袒,就這樣:

//radius:為半徑
//percent:進度值0-100
//color:進度顏色
<PercentageCircle style={{marginTop: 60}}
                                  radius={45}
                                  percent={this.state.progress3}
                                  color={"#f498db"}>
                </PercentageCircle>

執(zhí)行效果如下:

Untitled11.gif
有木有發(fā)現(xiàn)問題:

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);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末布讹,一起剝皮案震驚了整個濱河市琳拭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌描验,老刑警劉巖白嘁,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異挠乳,居然都是意外死亡权薯,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門睡扬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盟蚣,“玉大人,你說我怎么就攤上這事卖怜∈嚎” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵马靠,是天一觀的道長奄抽。 經(jīng)常有香客問我蔼两,道長,這世上最難降的妖魔是什么逞度? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任额划,我火速辦了婚禮,結(jié)果婚禮上档泽,老公的妹妹穿的比我還像新娘俊戳。我一直安慰自己,他們只是感情好馆匿,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布抑胎。 她就那樣靜靜地躺著,像睡著了一般渐北。 火紅的嫁衣襯著肌膚如雪阿逃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天赃蛛,我揣著相機與錄音恃锉,去河邊找鬼。 笑死焊虏,一個胖子當著我的面吹牛淡喜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播诵闭,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼炼团,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了疏尿?” 一聲冷哼從身側(cè)響起瘟芝,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎褥琐,沒想到半個月后锌俱,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡敌呈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年贸宏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磕洪。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡吭练,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出析显,到底是詐尸還是另有隱情鲫咽,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站分尸,受9級特大地震影響锦聊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜箩绍,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一孔庭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伶选,春花似錦史飞、人聲如沸尖昏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抽诉。三九已至陨簇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間迹淌,已是汗流浹背河绽。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留唉窃,地道東北人耙饰。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像纹份,于是被迫代替她去往敵國和親苟跪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容