import React, { Component } from 'react'; import { View, Image, StyleProp, ImageStyle, StyleSheet, ImageSourcePropType, ViewStyle, Animated, Easing, ImageResizeMode, TextStyle, } from 'react-native'; import Swiper, { SwiperProps } from 'react-native-swiper'; import GradientButton from './buttons/GradientButton'; import { Theme } from '../styles'; export interface GuideSwiperProps extends SwiperProps { /** 图片资源数组 */ imageSources: ImageSourcePropType[]; /** 点击函数 */ onPress?: (e?: any) => void; /** 图片父控件样式 */ imageContainerStyle?: StyleProp; /** 图片样式 */ imageStyle?: StyleProp; /** 图片拉伸模式, 默认contain */ imageResizeMode?: ImageResizeMode; /** 图片原图宽高比例, 默认为 750/1334 */ imageScale?: number; /** 指示点父控件样式 */ dotContainerStyle?: StyleProp; /** 按钮父控件样式 */ buttonContainerStyle?: StyleProp; /** 按钮样式, 默认 400*90 */ buttonStyle?: StyleProp; /** 按钮title样式 */ buttonTitleStyle?: StyleProp; /** 按钮标题, 默认是 立即体验 */ buttonTitle?: string; } interface GuideSwiperOwnState { index: number; fadeAnim: Animated.Value; } class GuideSwiper extends Component { static defaultProps = { imageScale: 750 / 1334, imageResizeMode : 'contain', height : Theme.DeviceHeight, width : Theme.DeviceWidth, imageSources: [], dotColor: Theme.theme, }; constructor(props) { super(props); this.state = { index: 0, fadeAnim: new Animated.Value(0), }; } _onIndexChanged = (index) => { this.setState({ index: index }); Animated.timing( this.state.fadeAnim, { toValue: index === this.props.imageSources.length - 1 ? 1 : 0, easing: Easing.linear, duration: 500, useNativeDriver: true, }, ).start(); } renderSwiper = () => { const { imageSources, height, width, style, imageContainerStyle, imageStyle, imageResizeMode, } = this.props; if (imageSources.length === 0) return ; return ( {imageSources.map((image, index) => ( ))} ); } paginationView = () => { const { imageSources, onPress, imageScale, buttonContainerStyle, dotContainerStyle, dotStyle, buttonStyle, buttonTitleStyle, buttonTitle, dotColor, } = this.props; if (imageSources.length === 0) return ; const marginH = (Theme.DeviceHeight - Theme.DeviceWidth / imageScale) * 0.5; if (this.state.index === imageSources.length - 1) { return ( ); } else { return ( {imageSources.map((image, index) => ( ))} ); } } render() { return ( {this.renderSwiper()} {this.paginationView()} {this.props.children} ); } } const Dot = ({ style, color }) => { return ; }; const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', }, imgContainer: { flex: 1, justifyContent: 'center', backgroundColor: Theme.white, }, img: { width: Theme.DeviceWidth, height: px2dp(1334), backgroundColor: Theme.white, }, btnContainer: { position: 'absolute', }, dotContainer: { position: 'absolute', flexDirection: 'row', }, dot: { width: px2dp(40), height: px2dp(6), borderRadius: px2dp(3), marginHorizontal: px2dp(10), }, }); export default GuideSwiper;