AnimationStaggerView

AnimationStaggerView.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Animated

} from 'react-native';

const arr = [];

for (var i = 0; i < 500; i++) {
  arr.push(i);
}

export default class AnimationStaggerView extends Component {
  constructor () {
    super();
    // 使用迭代器创建属性数组
    this.animatedValue = [];
    arr.forEach((value) => {
      this.animatedValue[value] = new Animated.Value(0);
    });
  }

  componentDidMount () {
    this.animate();
  }

  animate () {
     // 使用迭代器创建动画数组
    const animations = arr.map((item) => {
      return Animated.timing(
        this.animatedValue[item],
        {
          toValue: 1,
          duration: 2000,
        }
      );
    });
    Animated.stagger(10, animations).start();
  }

  render () {
    const animations = arr.map((a, i) => {
      console.log("a--------->"+ a);  
      console.log("i---------->"+ i);  
      //在这里a,i都是一样的 下标                                      
      return <Animated.View 
                 key={i} 
                 style={{opacity: this.animatedValue[i],  
                         height: 20, width: 20,
                        backgroundColor: 'red', 
                        marginLeft: 3, 
                        marginTop: 3}} />
    });

    return (
      <View style={styles.container}>
        {animations}
        <View></View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap'
  }
});
示例:

import React from 'react';
import {
   AppRegistry,
   View,
   Text,
   StyleSheet,
   Animated,
} from 'react-native';
import AnimationStaggerView from './AnimationStaggerView';

class MyFirstProject extends React.Component{

    render() {
        return (
             <View>
                 <AnimationStaggerView></AnimationStaggerView>
             </View>
        );
    }
}
AppRegistry.registerComponent('MyFirstProject', ()=> MyFirstProject);
效果图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值