1
+ /**
2
+ * Created by wangdi on 19/12/16.
3
+ */
4
+ 'use strict' ;
5
+
6
+ import React , { Component , PropTypes } from 'react' ;
7
+ import { Animated , ART } from 'react-native' ;
8
+ const { Surface} = ART ;
9
+ import Bar3 from '../shape/Bar3' ;
10
+ const AnimatedBar = Animated . createAnimatedComponent ( Bar3 ) ;
11
+
12
+ export default class NineCubesLoader extends Component {
13
+ constructor ( props ) {
14
+ super ( props ) ;
15
+ this . state = {
16
+ scales : [
17
+ new Animated . Value ( 0 ) ,
18
+ new Animated . Value ( 0 ) ,
19
+ new Animated . Value ( 0 ) ,
20
+ new Animated . Value ( 0 ) ,
21
+ new Animated . Value ( 0 )
22
+ ]
23
+ } ;
24
+ this . _animation = this . _animation . bind ( this ) ;
25
+ }
26
+
27
+ static propTypes = {
28
+ size : PropTypes . number ,
29
+ color : PropTypes . string
30
+ } ;
31
+
32
+ static defaultProps = {
33
+ size : 20 ,
34
+ color : '#1e90ff'
35
+ } ;
36
+
37
+ _renderCube ( i , j , scaleID ) {
38
+ const { size, color} = this . props ;
39
+ return (
40
+ < AnimatedBar
41
+ fill = { color }
42
+ width = { size }
43
+ height = { size }
44
+ x = { ( size / 2 ) * ( i * 2 + 1 ) }
45
+ y = { ( size / 2 ) * ( j * 2 + 1 ) }
46
+ scale = { this . state . scales [ scaleID ] }
47
+ />
48
+ ) ;
49
+ }
50
+
51
+ render ( ) {
52
+ const { size, color} = this . props ;
53
+ return (
54
+ < Surface width = { size * 3 } height = { size * 3 } >
55
+ { this . _renderCube ( 0 , 0 , 2 ) }
56
+ { this . _renderCube ( 0 , 1 , 1 ) }
57
+ { this . _renderCube ( 0 , 2 , 0 ) }
58
+ { this . _renderCube ( 1 , 0 , 3 ) }
59
+ { this . _renderCube ( 1 , 1 , 2 ) }
60
+ { this . _renderCube ( 1 , 2 , 1 ) }
61
+ { this . _renderCube ( 2 , 0 , 4 ) }
62
+ { this . _renderCube ( 2 , 1 , 3 ) }
63
+ { this . _renderCube ( 2 , 2 , 2 ) }
64
+ </ Surface >
65
+ ) ;
66
+ }
67
+
68
+ componentDidMount ( ) {
69
+ this . _animation ( ) ;
70
+ }
71
+
72
+ componentWillUnmount ( ) {
73
+ this . unmounted = true ;
74
+ }
75
+
76
+ _animation ( ) {
77
+ function seq ( self , i ) {
78
+ return Animated . sequence ( [
79
+ Animated . timing ( self . state . scales [ i ] , { toValue : 1 , duration : 300 , delay : ( i + 1 ) * 100 } ) ,
80
+ Animated . timing ( self . state . scales [ i ] , { toValue : 0 , duration : 300 , delay : 200 } )
81
+ ] )
82
+ }
83
+
84
+ Animated . parallel ( [
85
+ seq ( this , 0 ) , seq ( this , 1 ) , seq ( this , 2 ) , seq ( this , 3 ) , seq ( this , 4 ) ,
86
+ ] ) . start ( ( ) => {
87
+ if ( ! this . unmounted )
88
+ this . _animation ( ) ;
89
+ } ) ;
90
+ }
91
+ }
0 commit comments