-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (63 loc) · 1.95 KB
/
index.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
import React from 'react';
import { Text, View, Animated, Easing } from 'react-native';
// props [duration, height, containerStyle]
export default class RollingMessage extends React.Component {
constructor (props) {
super(props)
this.animate = this.animate.bind(this)
this.animatedValue = new Animated.Value(0)
this.height = props.height || 40
this.childrenHeight = props.childrenHeight || 20
this.centerPosition = (this.height - this.childrenHeight) / 2
this.dataIsArray = props.children.length && props.children.length > 1
this.state = {
currentIndex: 0
}
}
componentDidMount () {
this.animate()
}
animate () {
this.animatedValue.setValue(0)
Animated.timing(
this.animatedValue,
{
toValue: 1,
duration: this.props.duration || 2000,
easing: Easing.linear
}
).start(() => {
if (this.dataIsArray) {
this.setState({currentIndex: (this.state.currentIndex + 1) % this.props.children.length})
}
this.animate()
})
}
render() {
const { currentIndex } = this.state
const { containerStyle, childrenStyle } = this.props
const top = this.animatedValue.interpolate({
inputRange: [0, 0.2, 0.5, 0.8, 1],
outputRange: [0, this.centerPosition, this.centerPosition, this.centerPosition, this.height - this.childrenHeight]
})
const opacity = this.animatedValue.interpolate({
inputRange: [0, 0.2, 0.5, 0.8, 1],
outputRange: [0, 0.9, 1, 0.9, 0]
})
return (
<View style={[{
height: this.height,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
}, containerStyle]}>
<Animated.View style={[{
position: 'absolute',
height: this.childrenHeight,
top,
opacity
}, childrenStyle]}>{this.dataIsArray ? this.props.children[currentIndex] : this.props.children}</Animated.View>
</View>
);
}
}