forked from cheng-kang/react-native-lahk-marquee-label
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (152 loc) · 4.21 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { Component } from 'react';
import { View, Animated, Easing, Text } from 'react-native';
export default class MarqueeLabel extends Component {
state = {
textWidth: 0,
textHeight: 0,
bgViewWidth: 0,
duration: 0,
text: '',
animation: null,
};
componentWillMount() {
this.setState({
text: this.props.text || this.props.children || '',
});
this.animation = null;
this.animatedTransformX = new Animated.Value(0);
}
componentWillUnmount() {
if (this.state.animation !== null) {
this.state.animation.stop();
}
}
componentWillReceiveProps(nextProps) {
let newText = nextProps.text || nextProps.children || '';
let oldText = this.props.text || this.props.children || '';
if (newText !== oldText) {
this.state.animation.stop();
this.setState({
text: newText,
textWidth: 0,
textHeight: 0,
duration: 0,
animation: null,
});
}
}
componentDidUpdate(prevProps, prevState) {
let { textWidth, bgViewWidth, duration, animation } = this.state;
if (duration === 0) {
if (textWidth === 0 || bgViewWidth === 0) { return }
const { duration, speed } = this.props;
if (duration !== undefined) {
this.setState({
duration: duration,
});
} else if (speed !== undefined) {
this.setState({
duration: ((bgViewWidth + textWidth) / speed) * 1000,
});
}
} else {
if (animation === null) {
this.animatedTransformX.setValue(bgViewWidth);
this.setState({
animation: Animated.timing(this.animatedTransformX, {
toValue: -textWidth,
duration: duration,
useNativeDriver: true,
easing: Easing.linear,
}),
}, () => {
this.state.animation.start(() => {
this.setState({
animation: null,
});
});
});
}
}
}
textOnLayout(e) {
this.setState({
textWidth: e.nativeEvent.layout.width,
textHeight: e.nativeEvent.layout.height,
});
}
bgViewOnLayout(e) {
this.setState({
bgViewWidth: e.nativeEvent.layout.width,
});
}
render() {
const {
bgViewStyle, // Background View Custom Styles
textStyle, // Text Custom Styles
// Text Container Width:
// to make the text shown in one line, this value should be larger than text width
textContainerWidth = 1000,
// Text Container Height:
// to make the text shown in one line, this value should be larger than text height
// usually increase this value when text has a large font size.
textContainerHeight = 100,
textContainerStyle, // Text Container Custom Styles, not recommended to use
} = this.props;
const { textWidth, textHeight, text, animation } = this.state;
return (
<View
style={{ ...styles.bgViewStyle, ...bgViewStyle }}
onLayout={(event) => this.bgViewOnLayout(event)}
>
<View
style={{
...styles.textContainerStyle,
width: textContainerWidth,
height: textContainerHeight,
opacity: animation === null ? 0 : 1, // Make sure the view only shows when it's animating
...textContainerStyle,
}}
>
<Animated.Text
style={{
fontSize: 20,
transform: [{ translateX: this.animatedTransformX }],
width: textWidth,
height: textHeight,
...textStyle,
}}
>
{text}
</Animated.Text>
</View>
<Text
style={{
...styles.textSizeMeasuringViewStyle,
...textStyle,
}}
onLayout={(event) => this.textOnLayout(event)}
>
{text}
</Text>
</View>
);
}
}
const styles = {
bgViewStyle: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
overflow: 'scroll',
},
textContainerStyle: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
},
textSizeMeasuringViewStyle: {
opacity: 0,
fontSize: 20,
},
};