编写不易, 希望大家点赞

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 公告滚动组件(原生代码)的更多相关文章

  1. 深入浅出 React Native:使用 JavaScript 构建原生应用

    深入浅出 React Native:使用 JavaScript 构建原生应用 链接:https://zhuanlan.zhihu.com/p/19996445 原文:Introducing React ...

  2. [RN] React Native代码转换成微信小程序代码的转换引擎工具

    React Native代码转换成微信小程序代码的转换引擎工具 https://github.com/areslabs/alita

  3. React Native 项目常用第三方组件汇总

    React Native 项目常用第三方组件汇总 https://www.jianshu.com/p/d9cd9a868764?utm_campaign=maleskine&utm_conte ...

  4. React Navigation & React Native & React Native Navigation

    React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...

  5. React Native:使用 JavaScript 构建原生应用

    [转载] 本篇为联合翻译,译者:寸志,范洪春,kmokidd,姜天意 数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生 ...

  6. React Native知识5-Touchable类组件

    React Native 没有像web那样可以给元素绑定click事件,前面我们已经知道Text组件有onPress事件,为了给其他组件 也绑定点击事件,React Native提供了3个组件来做这件 ...

  7. React Native:使用 JavaScript 构建原生应用 详细剖析

    数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...

  8. Facebook 开源安卓版 React Native,开发者可将相同代码用于网页和 iOS 应用开发

    转自:http://mt.sohu.com/20150915/n421177212.shtml Facebook 创建了React Java 库,这样,Facebook 的工程团队就可以用相同的代码给 ...

  9. React Native 之 定义的组件 (跨文件使用)

    哈哈的~~~今天介绍的是自定义组件 然后去使用这个组件,让这个组件传递这各种文件之间  哈哈  下面开始吧!!!! 我们所要创建的是一个自定义的Button,先创建一个js文件起名为MyButton, ...

随机推荐

  1. mac 安装 报错 "/usr/local/include/stdint.h:2:10: error: #include nested too deeply"

    报错详细信息 构建错误 - “#include嵌套太深” /usr/local/include/stdint.h:2:10: error: #include nested too deeply #in ...

  2. js 常见数组算法

    数组方法概述 1.不改变原数组,返回新数组 concat() 连接两个或多个数组,两边的原始数组都不会变化,返回被连接数组的一个副本. join() 把数组中所有元素放入一个字符串中,返回字符串. s ...

  3. Angular发送广播和接收广播

    home.module.ts import {BroadcastService} from "../broadcast.service"; @NgModule({ imports: ...

  4. VMware安装VMwaretools

    默认点击“安装VMware Tools(T)”选项下载好安装包 下载的安装包放在计算机的media目录下 进入/media/ubuntu14-04/VMware Tools目录: cd /media/ ...

  5. CF1204E Natasha, Sasha and the Prefix Sums(组合数学)

    做法一 \(O(nm)\) 考虑\(f(i,j)\)为i个+1,j个-1的贡献 \(f(i-1,j)\)考虑往序列首添加一个\(1\),则贡献\(1\times\)为序列的个数:\(C(j+i-1,i ...

  6. Spring MVC国际化配置

    Spring MVC国际化配置 前言 项目开发中要考虑支持国际化,框架选用的是Spring MVC框架,那么问题来了Spring MVC如何配置并实现国际化. 实现过程(Maven项目) 对于Spri ...

  7. 数据结构Java版之邻接表实现图(十)

    邻接表实现图,实际上是在一个数组里面存放链表,链表存放的是连接当前节点的其他节点. package mygraph; import java.util.ArrayList; import java.u ...

  8. ubuntu之路——day6(今天对数据集的建立有了更深的体会)

    两个重点: 一.举个例子,如果建立一个图像识别的数据集,你的训练集和你的训练验证集是从网上爬下来的(也就是说这些图片的大小.像素.后期制作都可能很精美),你真正的测试集是用户的手机上传(不同的手机.环 ...

  9. c++ 函数后面加一个冒号的含义

    转载自:https://zhidao.baidu.com/question/2010930169328038188.html 冒号后面跟的是赋值,这种写法是C++的特性. A( int aa, int ...

  10. MD5与SHA1

    一.MD5 MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于 ...