-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
NumberTicker.js
140 lines (122 loc) · 3.56 KB
/
NumberTicker.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, {Component} from "react";
import {Animated, Easing, StyleSheet, Text, View} from "react-native";
import PropTypes from "prop-types";
const NumberTicker = ({style, textSize = 35, textStyle, number, duration}) => {
const mapToDigits = () => {
return (number + '').split('').map((data) => {
if (data === '.' || data === ',') {
return (
<Text key={data} style={[textStyle, {fontSize: textSize}]}>{data}</Text>
);
}
return (
<TextTicker
key={data}
textSize={textSize}
textStyle={textStyle}
targetNumber={parseFloat(data, 10)}
duration={duration}
/>
);
})
};
return (
<View style={style}>
<View style={{flexDirection: 'row'}}>
{mapToDigits()}
</View>
</View>
);
};
class TextTicker extends Component {
constructor(props) {
super(props);
this.state = {
animatedValue: new Animated.Value(0),
isAnimating: true,
delay: 800,
number: 1
};
const {targetNumber} = this.props;
if (this.props.targetNumber > 5) {
for (let i = 0; i <= targetNumber; i++) {
this.numberList.push({id: i});
}
} else {
for (let i = 9; i >= targetNumber; i--) {
this.numberList.push({id: i});
}
}
}
componentDidMount() {
this.startAnimation();
}
numberList = [];
startAnimation = () => {
const {animatedValue} = this.state;
Animated.timing(animatedValue, {
toValue: 1,
duration: this.props.duration,
easing: Easing.out(Easing.cubic),
useNativeDriver: true,
}).start(() => {
// on finish..
});
};
getInterpolatedVal = (val) => {
return val.interpolate({
inputRange: [0, 1],
outputRange: [this.props.textSize * this.numberList.length, this.props.textSize*0.2],
extrapolate: 'clamp',
});
};
renderNumbers = (styles) => {
return this.numberList.map((data) => {
return (
<Text key={data.id} style={[this.props.textStyle, styles.text]}>{data.id}</Text>
)
});
};
render() {
const {animatedValue} = this.state;
const styles = generateStyles(this.props.textSize);
return (
<View style={styles.container}>
<Animated.View style={{
transform: [{
translateY: this.getInterpolatedVal(animatedValue)
}]
}}>
{this.renderNumbers(styles)}
</Animated.View>
</View>
);
}
}
TextTicker.defaultProps = {
duration: 1800,
targetNumber: 7,
movingDown: true,
textSize: 35,
};
TextTicker.propTypes = {
duration: PropTypes.number,
targetNumber: PropTypes.number,
movingDown: PropTypes.bool,
textSize: PropTypes.number,
textStyle: PropTypes.any,
};
const generateStyles = (textSize) => StyleSheet.create({
container: {
width: textSize * 0.62,
height: textSize,
overflow: 'hidden',
alignItems: 'center',
justifyContent: 'flex-end',
},
text: {
fontSize: textSize,
lineHeight: textSize,
},
});
export default NumberTicker;