React Native豎向輪播組件的封裝詳解
本文主要介紹了React Native 通告訊息豎向輪播組件的封裝,具有一定的參考價值,有興趣的小夥伴們可以參考一下,希望能幫助到大家。
本文實例為大家分享了React Native通告訊息垂直輪播元件的封裝程式碼,供大家參考,具體內容如下
import React, {Component} from 'react' import { Text, View, Animated, Easing, StyleSheet, } from 'react-native' export default class ScrollVertical extends Component { static defaultProps = { enableAnimation: true, }; constructor(props) { super(props) let translateValue= new Animated.ValueXY({x: 0, y: 0}) translateValue.addListener(({x,y})=>{ // Log('value',x,y) }) this.state = { translateValue: translateValue, // 滚屏高度 scrollHeight: this.props.scrollHeight || 32, // 滚屏内容 kb_content: [], // Animated.View 滚动到的 y轴坐标 kb_tempValue: 0, // 最大偏移量 kb_contentOffsetY: 0, // 每一次滚动切换之前延迟的时间 delay: this.props.delay || 500, // 每一次滚动切换的持续时间 duration: this.props.duration || 500, enableAnimation: true, } } render() { return ( <View style={[styles.kbContainer, {height: this.state.scrollHeight}, this.props.kbContainer]}> { this.state.kb_content.length !== 0 ? <Animated.View style={[ {flexDirection: 'column'}, { transform: [ {translateY: this.state.translateValue.y} ] } ]}> {this.state.kb_content.map(this._createKbItem.bind(this))} </Animated.View> : null } </View> ) } componentWillReceiveProps(nextProps) { Log('componentWillReceiveProps', nextProps) this.setState({ enableAnimation: nextProps.enableAnimation?true:false }, () => { this.startAnimation(); } ) } componentDidMount() { Log('componentDidMount') let content = this.props.data || [] if (content.length !== 0) { let h = (content.length + 1) * this.state.scrollHeight this.setState({ kb_content: content.concat(content[0]), kb_contentOffsetY: h }) // 开始动画 // this._startAnimation() this.startAnimation(); } } _createKbItem(kbItem, index) { return ( <View key={index} style={[{justifyContent: 'center', height: this.state.scrollHeight}, this.props.scrollStyle]}> <Text style={[styles.kb_text_c, this.props.textStyle]}>{kbItem.content}</Text> </View> ) } startAnimation = () => { if (this.state.enableAnimation) { if(!this.animation){ this.animation = setTimeout(() => { this.animation=null; this._startAnimation(); }, this.state.delay); } } } componentWillUnmount() { if (this.animation) { clearTimeout(this.animation); } if(this.state.translateValue){ this.state.translateValue.removeAllListeners(); } } _startAnimation = () => { this.state.kb_tempValue -= this.state.scrollHeight; if (this.props.onChange) { let index = Math.abs(this.state.kb_tempValue) / (this.state.scrollHeight); this.props.onChange(index<this.state.kb_content.length-1?index:0); } Animated.sequence([ // Animated.delay(this.state.delay), Animated.timing( this.state.translateValue, { isInteraction: false, toValue: {x: 0, y: this.state.kb_tempValue}, duration: this.state.duration, // 动画持续的时间(单位是毫秒),默认为500 easing: Easing.linear } ), ]) .start(() => { // 无缝切换 // Log('end') if (this.state.kb_tempValue - this.state.scrollHeight === -this.state.kb_contentOffsetY) { // 快速拉回到初始状态 this.state.translateValue.setValue({x: 0, y: 0}); this.state.kb_tempValue = 0; } this.startAnimation(); }) } } const styles = StyleSheet.create({ kbContainer: { // 必须要有一个背景或者一个border,否则本身高度将不起作用 backgroundColor: 'transparent', overflow: 'hidden' }, kb_text_c: { fontSize: 18, color: '#181818', }
使用
import React, {Component} from 'react'; import { StyleSheet, View, TouchableOpacity, Alert, ScrollView, ART, TouchableHighlight, ListView, Dimensions, Text } from 'react-native'; import ScrollVertical from '../../app-widget/scroll-vertical' const dataArray = [ { title: '降价了', }, { title: '全场五折', }, { title: '打到骨折', } ] export default class extends React.Component { render() { let array = [{ content: '' }]; if (dataArray && dataArray.length > 0) { array = []; for (let item of dataArray) { array.push({ content: item.title}); } } return ( <View style={{ padding: Constant.sizeMarginDefault, paddingBottom: 0, backgroundColor: '#FFFFFF' }}> <TouchableOpacity onPress={() => { if (dataArray && dataArray.length > 0) { Log(dataArray[this.index].title) } }} style={{ flexDirection: 'row', backgroundColor: "#FFFFFF", alignItems: 'center', borderRadius: 8, paddingLeft: 5, paddingRight: 5 }}> <Text style={{ fontSize: Constant.scaleFontSize(14) }} fontWeight={'bold'}>公告</Text> <View style={{ marginLeft: 5, marginRight: 8, backgroundColor: '#b01638', borderRadius: 8, width: 22, alignItems: 'center', }}> <Text style={{ color: 'white', fontSize: Constant.fontSizeSmall }}>新</Text> </View> <View style={{ flexDirection: 'row', flex: 1 }}> <ScrollVertical onChange={(index => { this.index = index; })} enableAnimation={true} data={array} delay={2500} duration={1000} scrollHeight={34} scrollStyle={{ alignItems: 'flex-start' }} textStyle={{ color: Constant.colorTxtContent, fontSize: Constant.fontSizeSmall }} /> </View> <View style={{ height: 14, width: 1, backgroundColor: Constant.colorTxtContent }} /> <Text style={{ color: Constant.colorTxtContent, paddingLeft: Constant.sizeMarginDefault, fontSize: Constant.fontSizeSmall }}>查看</Text> </TouchableOpacity> </View> ); } };
#相關推薦:
以上是React Native豎向輪播組件的封裝詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

本站4月17日消息,集邦諮詢(TrendForce)近日發布報告,認為英偉達Blackwell新平台產品需求看漲,預估帶動台積電2024年CoWoS封裝總產能提升逾150%。英偉達Blackwell新平台產品包含B系列的GPU,以及整合英偉達自家GraceArmCPU的GB200加速卡等。集邦諮詢確認為供應鏈目前非常看好GB200,預估2025年出貨量預計超過百萬片,在英偉達高階GPU中的佔比達到40-50%。在英偉達計畫下半年交付GB200以及B100等產品,但上游晶圓封裝方面須進一步採用更複

PHP、Vue和React:如何選擇最適合的前端框架?隨著互聯網技術的不斷發展,前端框架在Web開發中起著至關重要的作用。 PHP、Vue和React作為三種代表性的前端框架,每一種都具有其獨特的特徵和優勢。在選擇使用哪種前端框架時,開發人員需要根據專案需求、團隊技能和個人偏好做出明智的決策。本文將透過比較PHP、Vue和React這三種前端框架的特徵和使

Java框架與React框架的整合:步驟:設定後端Java框架。建立專案結構。配置建置工具。建立React應用程式。編寫RESTAPI端點。配置通訊機制。實戰案例(SpringBoot+React):Java程式碼:定義RESTfulAPI控制器。 React程式碼:取得並顯示API回傳的資料。

本站7月9日訊息,AMDZen5架構「Strix」系列處理器會有兩種封裝方案,其中較小的StrixPoint將採用FP8封裝,而StrixHalo將會採用FP11封裝。圖源:videocardz訊息源@Olrak29_最新曝料稱StrixHalo的FP11封裝尺寸為37.5mm*45mm(1687平方毫米),和英特爾AlderLake、RaptorLakeCPU的LGA-1700封裝尺寸相同。 AMD最新的PhoenixAPU採用FP8封裝方案,尺寸為25*40mm,這意味著StrixHalo的F

透過封裝程式碼,C++函數可以提高GUI開發效率:程式碼封裝:函數將程式碼分組到獨立單元,使程式碼易於理解和維護。可重複使用性:函數可建立通用功能供應用程式中重複使用,減少重複編寫和錯誤。簡潔程式碼:封裝程式碼讓主邏輯簡潔,方便閱讀和除錯。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

React通過JSX與HTML結合,提升用戶體驗。 1)JSX嵌入HTML,使開發更直觀。 2)虛擬DOM機制優化性能,減少DOM操作。 3)組件化管理UI,提高可維護性。 4)狀態管理和事件處理增強交互性。

本站7月11日消息,經濟日報今天(7月11日)報道,富士康集團已進軍先進封裝領域,重點佈局時下主流的面板級扇出封裝(FOPLP)半導體方案。 1.繼旗下群創光電(Innolux)之後,富士康集團投資的夏普(Sharp)也宣布進軍日本面板級扇出式封裝領域,預計將於2026年投產。富士康集團在AI領域本身就有足夠的影響力,而補上先進封裝短板之後讓其可以提供「一條龍」服務,便於後續接受更多的AI產品訂單。本站查詢公開資料,富士康集團目前持有夏普10.5%的股權,該集團表示現階段不會增持,也不會減持,將維
