react native 文字輪播的實(shí)現(xiàn)示例
本著我為人人,人人為我的精神,敲過(guò)的代碼就要分享出來(lái)!
項(xiàng)目需要做一個(gè)文字的輪播,開(kāi)始想著是由下而上的滾動(dòng),但是還是寫(xiě)的不是很好,就先退而求其次,通過(guò)透明度來(lái)實(shí)現(xiàn)文字的滾動(dòng)了(也不能說(shuō)是滾動(dòng)了,是切換)。
為了貼上來(lái)還是寫(xiě)了些注釋的,也就不一一的解釋了,很簡(jiǎ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, // 下標(biāo) list: [], // 滾動(dòng)的列表 } } componentWillMount() { const { list } = this.props this.setState({ nowText: list[0].title, // 插入顯示的文本 list, // 滾動(dòng)的列表 }) this.onStart() // 啟動(dòng)計(jì)時(shí)器 A } onStart = () => { const { Intervals = 5000 } = this.props // Intervals 可為參數(shù)非必傳 this.timer = setInterval(() => { this.onDisappear() // 間隔Intervals毫秒啟動(dòng)計(jì)時(shí)器B }, Intervals) }; onDisappear = () => { this.timer1 = setInterval(() => { const { opacity, index, list } = this.state if (opacity === 0) { // 當(dāng)透明度為0時(shí)候開(kāi)始顯示在一個(gè)文本 if (index + 2 > list.length) { // 當(dāng)前顯示的文本為最后一個(gè)時(shí) 重頭再來(lái) this.setState({ nowText: list[0].title, index: 0, }) } else { this.setState({ nowText: list[index + 1].title, // 下一個(gè)文本 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: "失戀唱情歌,即是漏煤氣關(guān)窗", address: 4 }, ], } ... <TextLantern {...tProps} />
點(diǎn)擊跳轉(zhuǎn)說(shuō)一下我的做法:
通過(guò)當(dāng)前的 index 來(lái)拿出對(duì)應(yīng)的address進(jìn)行跳轉(zhuǎn)。
react要用的話(huà)改一下標(biāo)簽就好了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決react-native軟鍵盤(pán)彈出擋住輸入框的問(wèn)題
- react-native滑動(dòng)吸頂效果的實(shí)現(xiàn)過(guò)程
- react native基于FlatList下拉刷新上拉加載實(shí)現(xiàn)代碼示例
- react-native動(dòng)態(tài)切換tab組件的方法
- 詳解React Native 屏幕適配(炒雞簡(jiǎn)單的方法)
- React Native 自定義下拉刷新上拉加載的列表的示例
- 詳解react-native WebView 返回處理(非回調(diào)方法可解決)
- 詳解如何在項(xiàng)目中使用jest測(cè)試react native組件
- 關(guān)于React Native報(bào)Cannot initialize a parameter of type''NSArray<id<RCTBridgeModule>>錯(cuò)誤(解決方案)
相關(guān)文章
React Refs轉(zhuǎn)發(fā)實(shí)現(xiàn)流程詳解
Refs是一個(gè) 獲取 DOM節(jié)點(diǎn)或React元素實(shí)例的工具,在React中Refs 提供了一種方式,允許用戶(hù)訪(fǎng)問(wèn)DOM 節(jié)點(diǎn)或者在render方法中創(chuàng)建的React元素,這篇文章主要給大家介紹了關(guān)于React中refs的一些常見(jiàn)用法,需要的朋友可以參考下2022-12-12React路由跳轉(zhuǎn)的實(shí)現(xiàn)示例
在React中,可以使用多種方法進(jìn)行路由跳轉(zhuǎn),本文主要介紹了React路由跳轉(zhuǎn)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12React報(bào)錯(cuò)Element type is invalid解決案例
這篇文章主要為大家介紹了React報(bào)錯(cuò)Element type is invalid解決案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12react-native滑動(dòng)吸頂效果的實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于react-native滑動(dòng)吸頂效果的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用react-native具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06React?中?memo?useMemo?useCallback?到底該怎么用
在React函數(shù)組件中,當(dāng)組件中的props發(fā)生變化時(shí),默認(rèn)情況下整個(gè)組件都會(huì)重新渲染。換句話(huà)說(shuō),如果組件中的任何值更新,整個(gè)組件將重新渲染,包括沒(méi)有更改values/props的函數(shù)/組件。在react中,我們可以通過(guò)memo,useMemo以及useCallback來(lái)防止子組件的rerender2022-10-10