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

