React-native/React 公告滚动组件(原生代码)
编写不易, 希望大家点赞
import React, {PureComponent} from 'react';
import {Animated, Easing, View} from 'react-native';
export default class NoticeScroll extends PureComponent {
constructor(props) {
super(props);
this.state = {
newChildren: this.props.children,
};
this.animation = new Animated.Value();
this.direction = this.props.direction === 'vertical' ? 'height' : 'width';
this.transationValue = this.props.styles[this.direction];
this.key = ;
this.arr = [];
}
startAnimation() {
const meter = this.props.meter || ;
Animated.timing(this.animation, {
toValue: -this.transationValue + meter,
duration: this.props.scrolltime || ,
easing: Easing.linear,
useNativeDriver: true,
}).start(() => {
this.animation = new Animated.Value();
this.initPosition();
this.startAnimation();
});
}
initPosition() {
this.key++;
if (this.key < ) {
// React.Children.forEach(this.props.children, (child, index) => {
// let props = {
// key: `${this.key}${index}`,
// ...child.props
// };
// this.arr.push(React.cloneElement(child, props));
// });
React.Children.forEach(this.props.children, (child, index) => {
let newProps = {
key: `${this.key}${index}flag`,
...child.props,
};
this.arr.push(React.cloneElement(child, newProps));
});
}
this.setState({
newChildren: [...this.arr],
});
}
componentDidMount() {
this.initPosition();
this.startAnimation();
}
componentWillUnmount() {
this.startAnimation = () => {};
}
render() {
const {styles, direction} = this.props;
const {newChildren} = this.state;
return (
<View style={{overflow: 'hidden', height: , justifyContent: 'center'}}>
<Animated.View
style={{
transform: [
direction !== 'vertical'
? {translateX: this.animation}
: {translateY: this.animation},
],
flexDirection: 'row',
}}>
{newChildren}
</Animated.View>
</View>
);
}
}
组件可以在React中直接使用,把Animated.View 改成Animated.div即可
此代码有一个不好的地方,就是只能读取本地的,否则会有延迟
优化代码!!!
import React, { Component } from 'react';
import { View, Animated, Easing, Text, TouchableOpacity, InteractionManager } from 'react-native';
const styles = {
bgContainerStyle : {
flexDirection : 'row',
alignItems : 'center',
justifyContent : 'flex-start',
backgroundColor : '#FFFFFF',
overflow : 'hidden'
},
textMeasuringViewStyle: {
flexDirection : 'row',
opacity : ,
},
textMeasuringTextStyle: {
fontSize : ,
},
textStyle : {
fontSize : ,
color : '#000000',
}
};
export default class ScrollAnnounce extends Component {
constructor(props) {
super(props);
this.state = {
animation : null,
textList : [],
textWidth : ,
viewWidth : ,
start:false
}
}
static defaultProps = {
duration : ,
speed : ,
textList : [],
width : ,
height : ,
direction : 'left',
reverse : false,
separator : ,
onTextClick : () => {},
}
componentWillMount(){
this.setState({
textList : this.props.textList || [],
})
this.animatedTransformX = new Animated.Value();
}
componentDidUpdate(){
let { textWidth, viewWidth } = this.state;
let { duration, speed, width, direction } = this.props;
let mDuration = duration;
if(speed && speed > ){
mDuration = (width + textWidth) / speed * ;
}
if(!this.state.animation && textWidth && viewWidth){
this.animatedTransformX.setValue(direction == 'left' ? width : (direction == 'right' ? -textWidth : width));
this.setState({
animation : Animated.timing(this.animatedTransformX, {
toValue: direction == 'left' ? -textWidth : (direction == 'right' ? width : -textWidth),
duration: mDuration,
useNativeDriver: true,
easing: Easing.linear,
}),
}, () => {
this.state.animation && this.state.animation.start(() => {
this.setState({
animation: null,
});
});
})
}
}
componentWillReceiveProps(nextProps){
let newText = nextProps.textList || [];
let oldText = this.props.textList || [];
if (newText !== oldText) {
this.state.animation && this.state.animation.stop();
this.setState({
textList : newText,
animation: null,
start:true
});
}
}
componentWillUnmount(){
this.state.animation && this.state.animation.stop();
}
textOnLayout = (e) => {
let width = e.nativeEvent.layout.width;
let { textList, separator } = this.props;
this.setState({
textWidth : width + ((textList.length - ) * separator),
})
}
viewOnLayout = (e) => {
let width = e.nativeEvent.layout.width;
this.setState({
viewWidth : width,
})
}
textView(list){
let { textStyle, onTextClick, reverse, separator } = this.props;
let itemView = [];
for(let i = ;i<list.length;i++){
let item = list[i];
if(reverse){
item.value = item.value.split("").reverse().join("");
}
itemView.push(
<TouchableOpacity key = {''+i} activeOpacity = {0.9} onPress = {() => {
onTextClick(item)
}}>
<View style = {{flexDirection : 'row',marginRight : i < list.length - ? separator : }}>
<Text style = {{
...styles.textStyle,
...textStyle
}}
numberOfLines = {}
>{item.value}</Text>
</View>
</TouchableOpacity>
);
}
return(
<Animated.View
style = {{flexDirection : 'row',width : this.state.textWidth,transform: [{ translateX: this.animatedTransformX }]}}
onLayout={(event) => this.viewOnLayout(event)}
>
{itemView}
</Animated.View>
)
}
textLengthView(list){
let { textStyle } = this.props;
let text = '';
for(let i = ;i<list.length;i++){
text += list[i].value;
}
return(
<View style = {{
...styles.textMeasuringViewStyle,
width : list.length *
}}>
<Text style = {{
...styles.textMeasuringTextStyle,
...textStyle
}}
onLayout={(event) => this.textOnLayout(event)}
numberOfLines = {}
>{text}</Text>
</View>
)
}
render(){
let { width, height, bgContainerStyle } = this.props;
let { textList,start } = this.state;
return(
<View style = {{
...styles.bgContainerStyle,
width : width,
height : height,
...bgContainerStyle,
}} opacity = {this.state.animation ? : }>
{start&&this.textView(textList) }
{start&&this.textLengthView(textList) }
</View>
)
}
}
ScrollAnnounce
<ScrollAnnounce
textList = {noticeList}
speed = {}
width = {Platform.OS==='ios'?autoWidth():autoWidth()}
height = {}
direction = {'left'}
reverse = {false}
bgContainerStyle = {{backgroundColor : '#f8f8f8'}}
textStyle = {{fontSize : ,color : '#D1B793'}}
onTextClick = {(item) => {
this._toDetail(item)
}}
/>
React-native/React 公告滚动组件(原生代码)的更多相关文章
- 深入浅出 React Native:使用 JavaScript 构建原生应用
深入浅出 React Native:使用 JavaScript 构建原生应用 链接:https://zhuanlan.zhihu.com/p/19996445 原文:Introducing React ...
- [RN] React Native代码转换成微信小程序代码的转换引擎工具
React Native代码转换成微信小程序代码的转换引擎工具 https://github.com/areslabs/alita
- React Native 项目常用第三方组件汇总
React Native 项目常用第三方组件汇总 https://www.jianshu.com/p/d9cd9a868764?utm_campaign=maleskine&utm_conte ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
- React Native:使用 JavaScript 构建原生应用
[转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...
- React Native知识5-Touchable类组件
React Native 没有像web那样可以给元素绑定click事件,前面我们已经知道Text组件有onPress事件,为了给其他组件 也绑定点击事件,React Native提供了3个组件来做这件 ...
- React Native:使用 JavaScript 构建原生应用 详细剖析
数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...
- Facebook 开源安卓版 React Native,开发者可将相同代码用于网页和 iOS 应用开发
转自:http://mt.sohu.com/20150915/n421177212.shtml Facebook 创建了React Java 库,这样,Facebook 的工程团队就可以用相同的代码给 ...
- React Native 之 定义的组件 (跨文件使用)
哈哈的~~~今天介绍的是自定义组件 然后去使用这个组件,让这个组件传递这各种文件之间 哈哈 下面开始吧!!!! 我们所要创建的是一个自定义的Button,先创建一个js文件起名为MyButton, ...
随机推荐
- 简单理解php的socket编程【网摘】
php的socket编程算是比较难以理解的东西吧,不过,我们只要理解socket几个函数之间的关系,以及它们所扮演的角色,那么理解起来应该不是很难了,在笔者看来,socket编程,其实就是建立一个网络 ...
- leetcode解题报告(26):Add Binary
描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" ...
- [TJOI2019]甲苯先生和大中锋的字符串
有个叫asuldb的神仙来嘲讽我 说这题SAM水题,而且SA过不了 然后我就用SA过了 显然是一个Height数组上长为k的滑块,判一下两边,差分一下就可以了 #include"cstdio ...
- 64位内核第三讲,Windbg的使用.以及命令
目录 一丶驱动的调试. 1.线程 2.断点 3.内存查看命令 4.修改内存命令 5.栈相关操作命令 6.进程线程命令(内核命令) 一丶驱动的调试. 编写驱动免不了调试.所以这里介绍一下WinDbg的常 ...
- php 压缩文件
<?php $zip = new ZipArchive; $myfile = fopen("test.zip", "w"); chmod(); if ($ ...
- 在Typora中使用Latex
https://blog.csdn.net/happyday_d/article/details/83715440 今后可能更多在本地编辑出Markdown之后复制上博客或者上传到GitHub.
- 【CSP模拟赛】仔细的检查(树的重心&树hash)
题目描述 nodgd家里种了一棵树,有一天nodgd比较无聊,就把这棵树画在了一张纸上.另一天nodgd更无聊,就又画了一张. 这时nodgd发现,两次画的顺序是不一样的,这就导致了原本的某一个节点 ...
- div双击全屏,再双击恢复到原来的状态vue,js来做
需求是这样的: 有四个视频,视频是在4个区域,点击之后就全屏 <!DOCTYPE html> <html lang="en"> <head> & ...
- SpringBoot集成mybatis,同时读取一个数据库中多个数据表
SpringBoot集成mybatis,同时读取一个数据库中多个数据表: application.properties: mybatis.config-location=classpath:mybat ...
- 【转载】sudoers改坏后无法使用sudo的解决办法
练习安装odoo的时候,创建了一个odoo用户,想把它赋予sudo权限,然而,编辑的时候不留意,改坏了,导致sudo无法使用,无法编辑sudoers文件修改回来. 提示如下信息: >>&g ...