-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (123 loc) · 3.26 KB
/
index.js
File metadata and controls
153 lines (123 loc) · 3.26 KB
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
/**
* 文字跑马灯
* Author : Arno Ma
*
* example:
*
<TextCarousel>
<TextCarousel.Item>
<View><Text>1111111</Text></View>
</TextCarousel.Item>
<TextCarousel.Item>
<View><Text>22222222</Text></View>
</TextCarousel.Item>
<TextCarousel.Item>
<View><Text>33333333</Text></View>
</TextCarousel.Item>
</TextCarousel>
*
*
*/
import React from 'react';
import PropTypes, { func } from 'prop-types';
import { StyleSheet, View, Animated, Easing } from 'react-native';
class TextCarousel extends React.PureComponent {
constructor(props){
super(props);
this.state = {
anim: new Animated.ValueXY({x:0,y:-props.height})
}
}
render(){
let {height} = this.props;
let children = this.formatChildren();
let scrollH = children.length * height;
this.current = 1
this.total = children.length
return <View style={[styles.container,{height}]}>
<Animated.View style={[styles.scrollView,
{ height: scrollH, transform: this.state.anim.getTranslateTransform() }]}>
{ this.formatChildren() }
</Animated.View>
</View>
}
componentDidMount(){
let {interval, height, direction} = this.props;
let isUp = direction === 'up';
let cloneIndex = isUp ? this.total - 1 : 0;
let resetIndex = isUp ? 1 : this.total - 2;
let directionValue = isUp ? 1 : -1;
this.timer = setInterval(()=>{
this.current += directionValue
Animated.timing( this.state.anim, {
toValue: { x: 0, y: -this.current * height },
duration: 500,
easing: Easing.out(Easing.ease)
}).start(()=>{
if ( this.current === cloneIndex ) {
this.current = resetIndex
Animated.timing(this.state.anim,{
toValue: {x:0, y: -this.current * height},
duration: 0
}).start()
}
});
}, interval);
}
componentWillMount(){
this.timer && clearInterval(this.timer);
}
formatChildren(){
let {children, height} = this.props;
var result = [];
React.Children.forEach(children, (child, index)=>{
if (React.isValidElement(child) && child.type === Item){
result.push(
<View key={`tc-${index}`} style={{height}}>
{child}
</View>
)
}
})
// 克隆第一和最后一个元素
if (result.length > 0){
let firstItem = result[0]
let lastItem = result[result.length - 1]
result.unshift(React.cloneElement(lastItem, { key: `${lastItem.key}-last`}))
result.push(React.cloneElement(firstItem, { key: `${firstItem.key}-first`}))
}
return result
}
}
TextCarousel.defaultProps = {
height: 40,
interval: 4000,
direction: 'up'
}
TextCarousel.propTypes = {
height: PropTypes.number,
interval: PropTypes.number,
direction: PropTypes.oneOf(['up','down'])
}
class Item extends React.PureComponent {
render(){
return (
<View style={styles.itemStyle}>
{ React.Children.only(this.props.children) }
</View>
)
}
}
TextCarousel.Item = Item;
const styles = StyleSheet.create({
container: {
width: '100%',
overflow:'hidden'
},
itemStyle: {
flex:1
},
scrollView: {
}
})
export default TextCarousel;