React Native 之倒計(jì)時(shí)(定時(shí)器)控件

因?yàn)轫?xiàng)目購(gòu)物需要搶購(gòu)倒計(jì)時(shí)展示功能攻旦,所以封裝了個(gè)時(shí)分秒倒計(jì)時(shí)控件。把獲取的的時(shí)間戳換算成時(shí)分秒傳遞給控件,時(shí)間戳換算請(qǐng)看另一篇文章《React Native之計(jì)算服務(wù)器獲取時(shí)間戳與當(dāng)前時(shí)間的差值(算出天時(shí)分秒)》
控件展示.png

下邊看下控件的具體實(shí)現(xiàn):


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

const styles = StyleSheet.create({
    yzttext: {
        justifyContent: 'center',
        alignItems: 'center',
        marginLeft: 2,
        borderWidth: 1,
        flexDirection: 'row',
    },
});

export default class CountDownView extends Component {
   ###//item是自己定義的數(shù)據(jù)對(duì)象(包含時(shí)分秒具體數(shù)據(jù))
   ###//compentType//判別狀態(tài)
    static propTypes = {
        textColor: React.PropTypes.string,
        borderTextColor: React.PropTypes.string,
        borderColor: React.PropTypes.string,
        height: React.PropTypes.number,
        width: React.PropTypes.number,
        backgroundColor: React.PropTypes.string,
        fontSize: React.PropTypes.number,
        item: React.PropTypes.object,
        compentType: React.PropTypes.string,//判別狀態(tài)
    }
    constructor(props) {
        super(props);
     
        this.state = {
            item: this.props.item,
            Second: this.props.item.seconds,
            Minute: this.props.item.minutes,
            Hour: this.props.item.hours,
            SecondString: '',
            MinuteString: '',
            HourString: '',
        };
        ###//開(kāi)始倒計(jì)時(shí)
        this._startTimer();
    }

    componentWillReceiveProps(nextProps) {
      ###//獲取到傳遞的數(shù)據(jù)時(shí)便開(kāi)始倒計(jì)時(shí)
        this.setState({ item: nextProps.item, Second: nextProps.item.seconds, Minute: nextProps.item.minutes, Hour: nextProps.item.hours });
         this._startTimer();
    }

    _spliceString() {
        if (this.state.Second < 10) {
            this.setState({ SecondString: '0'.concat(this.state.Second.toString()) });
        } else {
            this.setState({ SecondString: this.state.Second.toString() });
        }
        if (this.state.Minute < 10) {
            this.setState({ MinuteString: '0'.concat(this.state.Minute.toString()) });
        } else {
            this.setState({ MinuteString: this.state.Minute.toString() });
        }
        if (this.state.Hour < 10) {
            this.setState({ HourString: '0'.concat(this.state.Hour.toString()) });
        } else {
            this.setState({ HourString: this.state.Hour.toString() });
        }
       
    }

    /**
     * 啟動(dòng)定時(shí)器倒計(jì)時(shí)
     * @returns {}
     * @private
     */
    _startTimer() {
          this.interval && clearInterval(this.interval);

        this.interval = setInterval(() => {
                this.state.Second -= 1;

                if (this.state.Second < 0) {
                    if (this.state.Minute >= 0) {
                        this.state.Second = 59;
                        // 分鐘需要減去一
                        this.state.Minute -= 1;
                    }

                    if (this.state.Minute < 0) {
                        if (this.state.Hour >= 0) {
                            this.state.Minute = 59;
                            this.state.Hour -= 1;
                        }

                        if (this.state.Hour < 0) {
                            this.state.Hour = 0;
                            this.state.Minute = 0;
                            this.state.Second = 0;
                            this.interval && clearInterval(this.interval);
                        }
                    }
                }

            this._spliceString();
        }, 1000);
    }
    componentDidMount() {

    }

    componentWillUnmount() {
         this.interval && clearInterval(this.interval);
    }

    _renderColonView(compentType) {
        //小點(diǎn)是文字
        if (compentType && compentType === 'goodDetail') {
            return (<Text style={{ marginLeft: 2, height: 25, width: 5, color: '#ffffff' }}>:</Text>);
        } else {
            //小點(diǎn)是圖片
            return (<Image
                            style={{ marginLeft: 2, height: 15, width: 3, resizeMode: 'cover' }}
                             defaultImage={require('../img/YZTAnyPurchase_CountDown_Colon.png')}
                             source={require('../img/YZTAnyPurchase_CountDown_Colon.png')}
            />);
        }
    }
    render() {
      //如果不傳遞參數(shù)量愧,則顯示默認(rèn)
        const borderTextColor = this.props.borderTextColor ? this.props.borderTextColor : '#ffffff';
        const borderColor = this.props.borderColor ? this.props.borderColor : '#333333';
        const height = this.props.height ? this.props.height : 55;
        const width = this.props.width ? this.props.width : 55;
        const backgroundColor = this.props.backgroundColor ? this.props.backgroundColor : 'rgba(0,0,0,0)';
        const fontSize = this.props.fontSize ? this.props.fontSize : 24;

        return (
            <View style={{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
                <View style={[styles.yzttext, { borderColor, height: height / 2, width: width / 2, backgroundColor }]}>
                    <Text style={{ fontSize: fontSize / 2, color: borderTextColor, backgroundColor }}>{this.state.HourString || '00'}</Text>
                </View>
                {this._renderColonView(this.props.compentType)}
                <View style={[styles.yzttext, { borderColor, height: height / 2, width: width / 2, backgroundColor }]}>
                    <Text style={{ fontSize: fontSize / 2, color: borderTextColor, backgroundColor }}>{this.state.MinuteString || '00'}</Text>
                </View>
                {this._renderColonView(this.props.compentType)}
                <View style={[styles.yzttext, { borderColor, height: height / 2, width: width / 2, backgroundColor }]}>
                    <Text style={{ fontSize: fontSize / 2, color: borderTextColor, backgroundColor }}>{this.state.SecondString || '00'}</Text>
                </View>
            </View>
        );
    }
}

使用控件的時(shí)候需要這樣暖哨,所傳的參數(shù)是props傳遞過(guò)來(lái)的沛慢。

item是包含時(shí)分秒的對(duì)象

/**
     * 倒計(jì)時(shí)view
     * @param type 類別
     * @param item data
     * @returns {}
     * @private
     */
    _renderFlashChildsView(type, item) {
        return (
            <ShoppingCountDownView
                item={item}
                compentType={this.props.compentType}
                textColor={this.props.textColor}
                borderTextColor={this.props.borderTextColor}
                borderColor={this.props.borderColor}
                height={this.props.height}
                width={this.props.width}
                backgroundColor={this.props.backgroundColor}
                fontSize={this.props.fontSize}
            />);
    }

使用的時(shí)候有什么地方不懂可以隨時(shí)留言匹厘。

代碼請(qǐng)戳這里略有修改https://github.com/miaozhang9/reactCountDownView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子膝擂,更是在濱河造成了極大的恐慌架馋,老刑警劉巖驶鹉,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件室埋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡孕蝉,警方通過(guò)查閱死者的電腦和手機(jī)腌逢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)霍殴,“玉大人系吩,你說(shuō)我怎么就攤上這事≡鲁冢” “怎么了科盛?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵贞绵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我冀泻,道長(zhǎng)蜡饵,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮焦辅,結(jié)果婚禮上筷登,老公的妹妹穿的比我還像新娘。我一直安慰自己狈醉,他們只是感情好惠险,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布班巩。 她就那樣靜靜地躺著,像睡著了一般逊桦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上强经,一...
    開(kāi)封第一講書(shū)人閱讀 52,696評(píng)論 1 312
  • 那天夕凝,我揣著相機(jī)與錄音户秤,去河邊找鬼鸡号。 笑死,一個(gè)胖子當(dāng)著我的面吹牛府蔗,可吹牛的內(nèi)容都是我干的汞窗。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼不铆,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼誓斥!你這毒婦竟也來(lái)了许帐?” 一聲冷哼從身側(cè)響起劳坑,我...
    開(kāi)封第一講書(shū)人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎成畦,沒(méi)想到半個(gè)月后距芬,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡羡鸥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年蔑穴,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惧浴。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡存和,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捐腿,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布茄袖,位于F島的核電站操软,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏宪祥。R本人自食惡果不足惜聂薪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蝗羊。 院中可真熱鬧藏澳,春花似錦、人聲如沸耀找。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)野芒。三九已至蓄愁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狞悲,已是汗流浹背撮抓。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摇锋,地道東北人胀滚。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像乱投,于是被迫代替她去往敵國(guó)和親咽笼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

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