React Native Animated實現的走馬燈/輪播動畫小示例
import React, { Component } from 'react';
import {
StyleSheet,
Animated,
Easing,
View,
Text,
} from 'react-native';
import Dimensions from 'Dimensions';
export default class extends Component {
constructor(props) {
super(props);
this.state = {
translateY: new Animated.Value(0),
};
}
componentDidMount() {console.log('componentDidMount')
this.showHeadBar(0, 5); //從第0條開始,輪播5條資料
}
showHeadBar(index, count) {console.log('start')
index++;
Animated.timing(this.state.translateY, {
toValue: -40 * index, //40為文字View的高度
duration: 300, //動畫時間
Easing: Easing.linear,
delay: 1500 //文字停留時間
}).start(() => { //每一個動畫結束後的回撥
if(index >= count) {
index = 0;
this.state.translateY.setValue(0);
}
this.showHeadBar(index, count); //迴圈動畫
})
}
render() {
return(
<View style={styles.container}>
<Animated.View
style={[styles.wrapper, {
transform: [{
translateY: this.state.translateY
}]
}
]}
>
<View style={styles.bar}>
<Text style={styles.barText}>1111</Text>
</View>
<View style={styles.bar}>
<Text style={styles.barText}>2222</Text>
</View>
<View style={styles.bar}>
<Text style={styles.barText}>3333</Text>
</View>
<View style={styles.bar}>
<Text style={styles.barText}>4444</Text>
</View>
<View style={styles.bar}>
<Text style={styles.barText}>5555</Text>
</View>
<View style={styles.bar}>
<Text style={styles.barText}>1111</Text>
</View>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: 5,
height: 40,
overflow: 'hidden',
backgroundColor: '#fff6ca',
borderRadius: 14,
},
wrapper: {
marginHorizontal: 5,
},
bar: {
height: 40,
justifyContent: 'center',
},
barText: {
width: Dimensions.get('window').width - 30 - 16,
marginLeft: 5,
color: '#ff7e00',
fontSize: 14,
},
});
```
注:需要注意的是最後一條額外加上的第一條,也即是說在view中放的是要顯示的文字在加上第一條文字,即111除了放在第一條還放在了最後