溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在react中使用native實現(xiàn)文字輪播

發(fā)布時間:2021-06-02 17:01:01 來源:億速云 閱讀:344 作者:Leah 欄目:web開發(fā)

怎么在react中使用native實現(xiàn)文字輪播?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

import React, { Component } from "react"
import { View, Text, TouchableOpacity } from "react-native"

export default class TextLantern extends Component {
  constructor(props) {
    super(props)
    this.state = {
      nowText: "", // 顯示的文本
      opacity: 1, // 透明度
      index: 0, // 下標
      list: [], // 滾動的列表
    }
  }

  componentWillMount() {
    const { list } = this.props
    this.setState({
      nowText: list[0].title, // 插入顯示的文本
      list, // 滾動的列表
    })
    this.onStart() // 啟動計時器 A
  }

  onStart = () => {
    const { Intervals = 5000 } = this.props // Intervals 可為參數(shù)非必傳
    this.timer = setInterval(() => {
      this.onDisappear() // 間隔Intervals毫秒啟動計時器B
    }, Intervals)
  };

  onDisappear = () => {
    this.timer1 = setInterval(() => {
      const { opacity, index, list } = this.state
      if (opacity === 0) {
        // 當透明度為0時候開始顯示在一個文本
        if (index + 2 > list.length) {
          // 當前顯示的文本為最后一個時 重頭再來
          this.setState({
            nowText: list[0].title,
            index: 0,
          })
        } else {
          this.setState({
            nowText: list[index + 1].title, // 下一個文本
            index: index + 1,
          })
        }
        this.onAppear() // 顯示
        clearInterval(this.timer1)
        return
      }
      this.setState({
        opacity: parseFloat(Math.abs(opacity - 0.04).toFixed(2)),
      })
    }, 20)
  };

  onAppear = () => {
    this.timer2 = setInterval(() => {
      const { opacity } = this.state
      if (opacity === 1) {
        clearInterval(this.timer2)
        return
      }
      this.setState({
        opacity: parseFloat(Math.abs(opacity + 0.04).toFixed(2)),
      })
    }, 20)
  };

  render() {
    const { nowText, opacity, list, index } = this.state
    return (
      <View style={{ borderWidth: 1, margin: 10, padding: 5, borderColor: "#dddddd" }}>
        <TouchableOpacity activeOpacity={0.75} onPress={() => {console.log(list[index].address)}}>
          <View style={{ width: "80%" }}>
            <Text
              style={{
                opacity,
                fontSize: 14,
              }}
            >
              {nowText}
            </Text>
          </View>
        </TouchableOpacity>
      </View>
    )
  }
}

引用:

const tProps = {
      list: [
        { id: 1, title: "不是這件事很難,而是每件事都難", address: 1 },
        { id: 2, title: "穩(wěn)食姐,犯法啊", address: 2 },
        { id: 3, title: "夜半訴心聲,何須太高清", address: 3 },
        { id: 4, title: "失戀唱情歌,即是漏煤氣關窗", address: 4 },
      ],
    }

...

<TextLantern {...tProps} />

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI