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 相关文章推荐
JS创建优美的页面滑动块效果 - Glider.js
Sep 27 Javascript
jquery1.4.2 for Visual studio 2010 模板文件
Jul 14 Javascript
jQuery中eq()方法用法实例
Jan 05 Javascript
js实现的动画导航菜单效果代码
Sep 10 Javascript
微信小程序模板之分页滑动栏
Feb 10 Javascript
Angular使用Md5加密的解决方法
Sep 16 Javascript
js中对象和面向对象与Json介绍
Jan 21 Javascript
详解Vue3.0 前的 TypeScript 最佳入门实践
Jun 18 Javascript
JS使用正则表达式判断输入框失去焦点事件
Oct 16 Javascript
微信小程序实现上拉加载功能
Nov 20 Javascript
Vue路由管理器Vue-router的使用方法详解
Feb 05 Javascript
微信小程序复选框实现多选一功能过程解析
Feb 14 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第一次无法获取cookie问题处理
2014/12/15 PHP
php使用Jpgraph绘制饼状图的方法
2015/06/10 PHP
php实现微信公众平台账号自定义菜单类
2015/10/11 PHP
让jQuery与其他JavaScript库并存避免冲突的方法
2013/12/23 Javascript
JavaScript按位运算符的应用简析
2014/02/04 Javascript
20条学习javascript的编程规范的建议
2014/11/28 Javascript
JavaScript实现列出数组中最长的连续数
2014/12/29 Javascript
javascript操作Cookie(设置、读取、删除)方法详解
2015/03/18 Javascript
基于jQuery实现仿QQ空间送礼物功能代码
2016/05/24 Javascript
jQuery焦点图轮播效果实现方法
2016/12/19 Javascript
layer实现关闭弹出层刷新父界面功能详解
2017/11/15 Javascript
element ui分页多选,翻页记忆的实例
2019/09/03 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
python学习笔记:字典的使用示例详解
2014/06/13 Python
linux下python使用sendmail发送邮件
2018/05/22 Python
Python面向对象之反射/自省机制实例分析
2018/08/24 Python
对python 通过ssh访问数据库的实例详解
2019/02/19 Python
python开发之anaconda以及win7下安装gensim的方法
2019/07/05 Python
Pycharm 字体大小调整设置的方法实现
2019/09/27 Python
python多线程高级锁condition简单用法示例
2019/11/07 Python
什么是Python中的匿名函数
2020/06/02 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
2020/06/08 Python
国际知名设计师时装商店:Coggles
2016/09/05 全球购物
惊艳的手工时装首饰:Migonne Gavigan
2018/02/23 全球购物
Myprotein法国官网:欧洲第一运动营养品牌
2019/03/26 全球购物
opencv实现图像几何变换
2021/03/24 Python
医院后勤自我鉴定
2013/10/13 职场文书
护士演讲稿范文
2014/01/05 职场文书
员工拓展培训方案
2014/02/15 职场文书
投标人廉洁自律承诺书
2014/05/26 职场文书
五好家庭申报材料
2014/12/20 职场文书
道德与公民自我评价
2015/03/09 职场文书
党委工作总结2015
2015/04/27 职场文书
班级元旦晚会开幕词
2016/03/04 职场文书
Html分层的box-shadow效果的示例代码
2021/03/30 HTML / CSS
css背景和边框标签实例详解
2021/05/21 HTML / CSS