CircleBox.js 2.2 KB
/**
 * Created by mac on 2017/5/10.
 */
import React, { Component} from 'react';
import {
    View,
    Text,
    StyleSheet
} from 'react-native';
import {AnimatedCircularProgress} from 'react-native-circular-progress';
import {getDataById} from '../../utils/excer';
import {observable, computed, action} from 'mobx';
import {observer} from 'mobx-react/native';
import {width} from '../../utils/getSize'
import PropTypes from 'prop-types';
@observer
export default class CircleBox extends Component {
    state = {
        progress:0
    };


    @action
    animate = () => {
        this.setState({progress:0});
        clearInterval(this.time);//取消定时器
        this.time = setInterval(() => {//启动定时器
            this.setState({progress:this.state.progress+100/60});
        }, 1000);
    };

    componentWillMount() {
        this.animate();
    }

    componentWillReceiveProps() {
        // this.animate()
    }

    componentWillUnmount() {
        clearInterval(this.time);
    }

    calcNum = () => {
        let num = this.state.progress*60/100;
        // console.warn("num = " + num);
        if (num < 60) {
            return num.toFixed(0) + "s";
        } else if(num < 3600){
            return (num / 60).toFixed(0) + "m";
        }else{
            return (num / 3600).toFixed(0) + "h";
        }
    }

    render() {
        return (
            <View style={{width, height: 130, justifyContent: 'center', alignItems: 'center'}}>
              <AnimatedCircularProgress
                  size={110}
                  width={4}
                  fill={this.state.progress%100}
                  tintColor="#e52d43"
                  rotation={0}
                  backgroundColor="#dddddd"

              >
                  {
                      () => (
                          <Text style={styles.font}>
                              {this.calcNum()}
                          </Text>
                      )
                  }
              </AnimatedCircularProgress>
            </View>
        )
    }
}
const styles = StyleSheet.create({

    font: {
        fontSize: 35,
        color: '#999999',
        textAlign: 'center',
        marginTop: -5,
        backgroundColor: 'transparent'
    }
});