【RN】跑馬燈組件實(shí)例

外層調(diào)用


let text=[
            {text:'1111',link:'1234124'},
            {text:'2222',link:'1234124'},
        ]

  <Carousel text />

import React, {Component} from 'react';
import {
    View,
    Text,
    Image, TouchableOpacity,
    StyleSheet, Animated
} from 'react-native';
import {Images} from "../util/Images";

const maxTop = 50
export default class Carousel extends Component {

    static defaultProps = {
        style: {}, //組件容器paddingTop的值
        text: '', //跑馬燈顯示的文字, 如果傳入數(shù)組骏掀,則有跑馬燈特效
        to: 'bottom', //跑馬燈滾動(dòng)方向毫玖,默認(rèn)從上向下 top/bottom
        height: 40, //跑馬燈的默認(rèn)高度,
        time: 4000,//多長時(shí)間滾動(dòng)一次,默認(rèn)4s
        duration:2000,//動(dòng)畫持續(xù)時(shí)間
    }

    constructor(props) {
        super(props);
        this.state = {
            textArr: [],   //自定義SideToast內(nèi)容
        }
        this.maxTop = this.props.height / 2 + this._getFontSize() / 2 //當(dāng)前展示文字滾動(dòng)出去所需的top=容器高度/2 +字的大小/2

        this.showViewTop1 = new Animated.Value(0)
        this.showViewTop2 = new Animated.Value(-this.maxTop)
        this.isArray=false
    }

    _getFontSize = () => {
        if (this.props.style && this.props.style.fontSize) {
            let fontSize = parseInt(this.props.fontSize)
            return fontSize
        }

        if (styles && styles.textStyle) {
            return styles.textStyle.fontSize
        }
    }

    componentDidMount() {
        const {text}=this.props

        if (text instanceof Array && text.length>1) {
            console.log('==hugo 是數(shù)組:'+JSON.stringify(text))
            this.setState({
                textArr:text
            },()=>{
                this.startAnimateTimer()
            })
        }else{
            console.log('==hugo 是字符串')
            this.setState({
                textArr:[{text,link:''}]
            })
        }


    }
    startAnimateTimer=()=>{
        let {time,duration}=this.props
        const {textArr}=this.state
        let _textArr=textArr
        this.timer=setTimeout(()=>{
            Animated.parallel([
                Animated.spring(this.showViewTop1, {
                    toValue: this.maxTop,
                    duration,
                    tension:10,
                }),
                Animated.spring(this.showViewTop2, {
                    toValue: 0,
                    duration,
                    tension:10,
                })
            ]).start(()=>{
                //動(dòng)畫完成時(shí)的回調(diào)
                this.showViewTop1=new Animated.Value(-this.maxTop)
                _textArr.push(_textArr[0])
                _textArr[0]=!!_textArr[2]?_textArr[2]:_textArr[1]
                this.setState({textArr:[..._textArr]},()=>{
                    _textArr=this.state.textArr
                    console.log('!!!!1:'+JSON.stringify(_textArr))
                    setTimeout(()=>{
                        Animated.parallel([
                            Animated.spring(this.showViewTop1, {
                                toValue: 0,
                                duration:2000,
                                tension:10,
                            }),
                            Animated.spring(this.showViewTop2, {
                                toValue:this.maxTop,
                                duration,
                                tension:10,
                            })
                        ]).start(()=>{
                            //動(dòng)畫完成時(shí)的回調(diào)
                            this.showViewTop2=new Animated.Value(-this.maxTop)
                            _textArr.push(_textArr[1])
                            _textArr.shift()
                            _textArr.shift()
                            this.setState({textArr:[..._textArr]},()=>{
                                console.log('!!!!2:'+JSON.stringify(_textArr))
                                this.startAnimateTimer()
                            })
                        })
                    },time-duration)
                })

            })
        },time-duration)
    }

    renderTextView = () => {
        const {textArr}=this.state
        console.log('==hugo arr:'+JSON.stringify(textArr))
        if (textArr.length>1) {
            //是數(shù)組施无,采用跑馬燈的效果
            return <View style={{flex:1}}>
                <Animated.View style={[{position: 'absolute'},{width: '90%', top: this.showViewTop2}]}>
                    <Text numberOfLines={1}
                          ellipsizeMode={'tail'}
                          style={[styles.textStyle]}>
                        {textArr[1]&&textArr[1].text}
                    </Text>
                </Animated.View>


                <Animated.View style={{width: '90%',top: this.showViewTop1}}>
                    <Text numberOfLines={1}
                          ellipsizeMode={'tail'}
                          style={[styles.textStyle]}>
                        {textArr[0]&&textArr[0].text}
                    </Text>
                </Animated.View>
            </View>
        }else{
            //是字符串,無跑馬燈效果,取數(shù)組的第一個(gè)元素
            return  <Text numberOfLines={1}
                  ellipsizeMode={'tail'}
                  style={[styles.textStyle, {width: '90%'}]}>
                {textArr[0] && textArr[0].text}
            </Text>
        }

    }

    render() {
        const {top, height} = this.props
        return <View style={[styles.container, {paddingTop: top, height}, this.props.style]}>

            {this.renderTextView()}

            <View style={{flex: 1}}>
                <Image style={styles.imageStyle} resizeMode={'stretch'} source={require('XXXX')}/>
            </View>
        </View>

    }
}
const styles = StyleSheet.create({
    container: {
        backgroundColor: '#f8e15f',
        borderRadius: 5,
        flexDirection: 'row',
        alignItems: 'center',
        overflow:'hidden'
    },
    textStyle: {
        marginLeft: 8,
        fontSize: 16
    },
    imageStyle: {
        alignSelf: 'flex-end',
        width: 36,
        height: 36
    }
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末侮叮,一起剝皮案震驚了整個(gè)濱河市捐寥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌我注,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迟隅,死亡現(xiàn)場離奇詭異但骨,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)智袭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進(jìn)店門奔缠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人吼野,你說我怎么就攤上這事校哎。” “怎么了瞳步?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵闷哆,是天一觀的道長。 經(jīng)常有香客問我单起,道長抱怔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任嘀倒,我火速辦了婚禮屈留,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘括儒。我一直安慰自己绕沈,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布帮寻。 她就那樣靜靜地躺著,像睡著了一般赠摇。 火紅的嫁衣襯著肌膚如雪固逗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天藕帜,我揣著相機(jī)與錄音烫罩,去河邊找鬼。 笑死洽故,一個(gè)胖子當(dāng)著我的面吹牛贝攒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播时甚,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼隘弊,長吁一口氣:“原來是場噩夢啊……” “哼哈踱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起梨熙,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤开镣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后咽扇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邪财,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年质欲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了树埠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嘶伟,死狀恐怖怎憋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奋早,我是刑警寧澤盛霎,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站耽装,受9級特大地震影響愤炸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜掉奄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一规个、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧姓建,春花似錦诞仓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涣狗,卻和暖如春谍婉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背镀钓。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工穗熬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人丁溅。 一個(gè)月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓唤蔗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子妓柜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評論 2 348

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