forked from oblador/react-native-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeAnimatable.js
117 lines (107 loc) · 3.22 KB
/
makeAnimatable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
var React = require('react-native');
var {
PropTypes,
Animated,
Easing,
} = React;
var makeAnimatable = function(Component, indeterminateProgress) {
var AnimatedComponent = Animated.createAnimatedComponent(Component);
return React.createClass({
propTypes: {
animated: PropTypes.bool,
indeterminate: PropTypes.bool,
progress: PropTypes.number.isRequired,
direction: PropTypes.oneOf(['clockwise', 'counter-clockwise']),
},
getDefaultProps: function() {
return {
animated: true,
indeterminate: false,
progress: 0,
direction: 'clockwise',
};
},
getInitialState: function() {
var progress = Math.min(Math.max(this.props.progress, 0), 1);
var animationValue = new Animated.Value(progress);
return {
progress,
animationValue,
rotation: new Animated.Value(0),
};
},
componentWillReceiveProps: function(props) {
if(props.indeterminate !== this.props.indeterminate) {
if(props.indeterminate) {
this.spin();
} else {
Animated.spring(this.state.rotation, {
toValue: (this._rotation > 0.5 ? 1 : 0),
}).start(endState => {
if(endState.finished) {
this.state.rotation.setValue(0);
}
});
}
}
var progress = (props.indeterminate ? indeterminateProgress || 0 : Math.min(Math.max(props.progress, 0), 1));
if(progress !== this.state.progress) {
if(props.animated) {
Animated.spring(this.state.animationValue, {
toValue: progress,
bounciness: 0,
}).start();
} else {
this.setState({ progress });
}
}
},
spin: function() {
this.state.rotation.setValue(0);
Animated.timing(this.state.rotation, {
toValue: this.props.direction === 'counter-clockwise' ? -1 : 1,
duration: 1000,
easing: Easing.linear,
isInteraction: false,
}).start(endState => {
if(endState.finished) {
this.spin();
}
});
},
componentDidMount: function() {
this.state.animationValue.addListener(event => this.setState({ progress: event.value }));
this.state.rotation.addListener(event => this._rotation = event.value );
if(this.props.indeterminate) {
this.spin();
if(indeterminateProgress) {
Animated.spring(this.state.animationValue, {
toValue: indeterminateProgress,
}).start();
}
}
},
componentWillUnmount: function() {
this.state.animationValue.removeAllListeners();
},
render: function() {
var { progress, size, style, children, ...props } = this.props;
return (<AnimatedComponent
progress={this.state.progress}
size={size}
style={[{
backgroundColor: 'transparent',
overflow: 'hidden',
transform: [{
rotate: this.state.rotation.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
})
}]
}, style]}
{...props}>{children}</AnimatedComponent>);
}
});
};
module.exports = makeAnimatable;