React Native 通告消息竖向轮播组件的封装


Posted in Javascript onAugust 25, 2020

本文实例为大家分享了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 通告消息竖向轮播组件的封装

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javaScript矢量图表库-gRaphael几行代码实现精美的条形图/饼图/点图/曲线图
Jan 09 Javascript
jQuery获得IE版本不准确webbrowser的解决方法
Feb 23 Javascript
js实现同一页面多个运动效果的方法
Apr 10 Javascript
ArtEditor富文本编辑器增加表单提交功能
Apr 18 Javascript
JS去除重复并统计数量的实现方法
Dec 15 Javascript
jQuery插件form-validation-engine正则表达式操作示例
Feb 09 Javascript
浅析JavaScript中var that=this
Feb 17 Javascript
详解Vue2 SSR 缓存 Api 数据
Nov 20 Javascript
使用layui 渲染table数据表格的实例代码
Aug 19 Javascript
Vue页面跳转动画效果的实现方法
Sep 23 Javascript
vue--vuex详解
Apr 15 Javascript
原生JavaScript创建不可变对象的方法简单示例
May 07 Javascript
Vue v2.5 调整和更新不完全问题
Oct 24 #Javascript
Vue.js 2.5新特性介绍(推荐)
Oct 24 #Javascript
Vue 2.5 Level E 发布了: 新功能特性一览
Oct 24 #Javascript
浅谈在vue项目中如何定义全局变量和全局函数
Oct 24 #Javascript
Angularjs添加排序查询功能的实例代码
Oct 24 #Javascript
详解基于Vue+Koa的pm2配置
Oct 24 #Javascript
Vue.js2.0中的变化小结
Oct 24 #Javascript
You might like
php初学者写及时补给skype用户充话费的小程序
2008/11/02 PHP
php 生成静态页面的办法与实现代码详细版
2010/02/15 PHP
PHP eval函数使用介绍
2013/12/08 PHP
PHP简单判断手机设备的方法
2016/08/23 PHP
php-fpm服务启动脚本的方法
2018/04/27 PHP
表单项的name命名为submit、reset引起的问题
2007/12/22 Javascript
经典海量jQuery插件 大家可以收藏一下
2010/02/07 Javascript
javascript 实现键盘上下左右功能的小例子
2013/09/15 Javascript
数据分析软件之FineReport教程:[5]参数界面JS(全)
2015/08/13 Javascript
js支持键盘控制的左右切换立体式图片轮播效果代码分享
2015/08/26 Javascript
JS实现漂亮的淡蓝色滑动门效果代码
2015/09/23 Javascript
JS高仿抛物线加入购物车特效实现代码
2017/02/20 Javascript
微信小程序中的onLoad详解及简单实例
2017/04/05 Javascript
Iphone手机、安卓手机浏览器控制默认缩放大小的方法总结(附代码)
2017/08/18 Javascript
详解vue-cli脚手架中webpack配置方法
2018/08/22 Javascript
浅析Python基础-流程控制
2016/03/18 Python
Python的Flask框架中集成CKeditor富文本编辑器的教程
2016/06/13 Python
Python爬虫信息输入及页面的切换方法
2018/05/11 Python
python实现泊松图像融合
2018/07/26 Python
在PyCharm中实现关闭一个死循环程序的方法
2018/11/29 Python
python实现AES加密解密
2019/03/28 Python
对python中GUI,Label和Button的实例详解
2019/06/27 Python
在python中创建指定大小的多维数组方式
2019/11/28 Python
Win10下用Anaconda安装TensorFlow(图文教程)
2020/06/18 Python
Keras-多输入多输出实例(多任务)
2020/06/22 Python
python制作微博图片爬取工具
2021/01/16 Python
定义css设备类型-Media Queries图表简介及使用方法
2013/01/21 HTML / CSS
使用CSS禁止textarea调整大小功能的方法
2015/03/13 HTML / CSS
Shell编程面试题
2012/05/30 面试题
事业单位公务员的职业生涯规划
2014/01/15 职场文书
优秀学生评语大全
2014/04/25 职场文书
社区工作者演讲稿
2014/05/23 职场文书
领导离职感言
2015/08/03 职场文书
Python入门学习之类的相关知识总结
2021/05/25 Python
叶县这家生产军用电台的兵工厂,人称“四机部”,走出一上将
2022/02/18 无线电
vue递归实现树形组件
2022/07/15 Vue.js