推荐使用:React Native FadingSlides Component - 创新的滑动循环组件
import React from 'react';
import {
View,
Text,
StyleSheet,
Dimensions,
Image,
TouchableOpacity,
} from 'react-native';
import FadingSlides from 'react-native-fading-slides';
const { width, height } = Dimensions.get('window');
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<FadingSlides
data={data}
renderItem={({ item, index }) => (
<View style={styles.slide}>
<Image style={styles.image} source={item.image} />
<Text style={styles.text}>{item.text}</Text>
</View>
)}
width={width}
height={height / 2}
entryAnimation="fadeIn"
exitAnimation="fadeOut"
infinite={true}
/>
</View>
);
}
}
const data = [
{
text: 'Slide 1',
image: require('./images/slide1.jpg'),
},
{
text: 'Slide 2',
image: require('./images/slide2.jpg'),
},
// ...
];
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
slide: {
justifyContent: 'center',
alignItems: 'center',
},
image: {
width,
height: height / 2,
resizeMode: 'cover',
},
text: {
color: 'white',
fontSize: 20,
fontWeight: 'bold',
},
});
这个代码实例展示了如何使用FadingSlides
组件来创建一个带有淡入淡出动画的滑动轮播组件。数据和样式被抽象出来,使得代码更加清晰和可维护。此外,图片资源使用require
方法加载,确保了在打包时图片资源能够被正确处理。
评论已关闭