|
| 1 | +/** |
| 2 | + * Created by wangdi on 8/12/16. |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import React, { Component } from 'react'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | +import {Animated, ART, Easing} from 'react-native'; |
| 9 | +const {Surface} = ART; |
| 10 | +import AnimatedCircle from '../animated/AnimatedCircle'; |
| 11 | + |
| 12 | +export default class OpacityDotsLoader extends Component { |
| 13 | + static propTypes = { |
| 14 | + color: PropTypes.string, |
| 15 | + size: PropTypes.number, |
| 16 | + betweenSpace: PropTypes.number |
| 17 | + }; |
| 18 | + |
| 19 | + static defaultProps = { |
| 20 | + color: '#1e90ff', |
| 21 | + size: 10, |
| 22 | + betweenSpace: 5 |
| 23 | + }; |
| 24 | + |
| 25 | + constructor(props) { |
| 26 | + super(props); |
| 27 | + this.state = { |
| 28 | + opacity: [new Animated.Value(0.5), new Animated.Value(0.5), new Animated.Value(0.5)] |
| 29 | + }; |
| 30 | + this._animation = this._animation.bind(this); |
| 31 | + } |
| 32 | + |
| 33 | + _renderCircle(i) { |
| 34 | + const {color, size, betweenSpace} = this.props; |
| 35 | + return ( |
| 36 | + <AnimatedCircle radius={size} fill={color} x={size/2 + i * (size+betweenSpace)} y={size/2} opacity={this.state.opacity[i]} scale={1}/> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + render() { |
| 41 | + const {size, betweenSpace} = this.props; |
| 42 | + return ( |
| 43 | + <Surface width={size*3 + betweenSpace*2} height={size}> |
| 44 | + {this._renderCircle(0)} |
| 45 | + {this._renderCircle(1)} |
| 46 | + {this._renderCircle(2)} |
| 47 | + </Surface> |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + componentDidMount() { |
| 52 | + this._animation(); |
| 53 | + } |
| 54 | + |
| 55 | + componentWillUnmount() { |
| 56 | + this.unmounted = true; |
| 57 | + } |
| 58 | + |
| 59 | + _animation() { |
| 60 | + function seq(self, i) { |
| 61 | + return Animated.sequence([ |
| 62 | + Animated.timing(self.state.opacity[i], {toValue: 1, duration: 500, delay: i*500}), |
| 63 | + Animated.timing(self.state.opacity[i], {toValue: 0.3, duration: 500, delay: 500}) |
| 64 | + ]) |
| 65 | + } |
| 66 | + |
| 67 | + Animated.parallel([ |
| 68 | + seq(this, 0), seq(this, 1), seq(this, 2) |
| 69 | + ]).start(() => { |
| 70 | + if (!this.unmounted) |
| 71 | + this._animation(); |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
0 commit comments