|
1 | 1 | import React, { useState, useEffect, useRef, useCallback, } from 'react' |
2 | | -import { StyleSheet, View, Image } from 'react-native'; |
| 2 | +import { StyleSheet, View, Image, ScrollView, Dimensions, TouchableOpacity, NativeSyntheticEvent, NativeScrollEvent } from 'react-native'; |
3 | 3 | export interface dataSourceType { |
4 | 4 | url: string |
5 | | - // onClick?: () => void |
| 5 | + onClick?: () => void |
6 | 6 | } |
| 7 | +export type dotType = 'dot' | 'block' |
7 | 8 | export interface SwiperProps { |
8 | | - dataSource: dataSourceType[]; |
| 9 | + // 数据源 |
| 10 | + dataSource?: dataSourceType[]; |
| 11 | + // 轮播图宽度 |
| 12 | + width?: number, |
| 13 | + // 轮播图高度 |
| 14 | + height?: number, |
| 15 | + // 播放时间 |
9 | 16 | time?: number; |
10 | | - width?: number; |
11 | | - height?: number; |
12 | | - autoplay?: boolean |
| 17 | + // 圆角边框 |
| 18 | + borderRadius?: number, |
| 19 | + // 是否开启自动播放 |
| 20 | + autoplay?: boolean, |
| 21 | + // 指示点样式 dot: 圆点 block: 方块 |
| 22 | + dotStyle?: dotType |
13 | 23 | } |
14 | 24 | const Swiper = (porps: SwiperProps) => { |
15 | | - const { dataSource = [], time = 3000, autoplay = true, width = "100%", height = 130 } = porps |
| 25 | + const gitwidth = Dimensions.get('window').width; |
| 26 | + const { dataSource = [], width = gitwidth, height = 130, time = 3000, autoplay = true, borderRadius = 0, dotStyle = "dot" } = porps |
16 | 27 | let [curIndex, setCurIndex] = useState(0) |
17 | 28 | let timer = useRef<NodeJS.Timeout | undefined>(); |
| 29 | + const scrollToRef: any = useRef() |
| 30 | + |
| 31 | + // 自动播放 |
18 | 32 | const autoPlay = useCallback(() => { |
19 | 33 | clearInterval(timer.current as unknown as number); |
20 | 34 | timer.current = setInterval(() => { |
| 35 | + let index = curIndex + 1 |
21 | 36 | if (curIndex === dataSource.length - 1) { |
| 37 | + index = 0 |
22 | 38 | setCurIndex(0); |
23 | 39 | } else { |
24 | 40 | setCurIndex(curIndex + 1); |
25 | 41 | } |
| 42 | + scrollToRef.current.scrollTo({ x: width * index, y: 0, animated: true }) |
26 | 43 | }, time); |
27 | | - }, [dataSource, curIndex]); |
| 44 | + }, [curIndex]); |
| 45 | + |
| 46 | + // 开启播放 |
28 | 47 | useEffect(() => { |
29 | | - if (autoplay) { |
30 | | - autoPlay() |
31 | | - } |
| 48 | + if (autoplay) autoPlay() |
32 | 49 | }, [autoPlay]); |
| 50 | + |
| 51 | + // 页面离开停止播放 |
33 | 52 | useEffect(() => { |
34 | 53 | return () => { |
35 | 54 | clearInterval(timer.current as unknown as number); |
36 | 55 | }; |
37 | 56 | }, []); |
| 57 | + |
| 58 | + // 开始拖拽终止定时器 |
| 59 | + const onScrollBeginDrag = () => { |
| 60 | + if (autoplay) clearInterval(timer.current as unknown as number) |
| 61 | + } |
| 62 | + |
| 63 | + // 停止拖拽开启定时器 |
| 64 | + const onScrollEndDrag = () => { |
| 65 | + if (autoplay) autoPlay() |
| 66 | + } |
| 67 | + |
| 68 | + // 拖拽成功 |
| 69 | + const onMomentumScrollEnd = (e: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 70 | + e.persist(); |
| 71 | + let offSetX = e.nativeEvent.contentOffset.x; |
| 72 | + let mentWidth = e.nativeEvent.layoutMeasurement.width; |
| 73 | + let page = offSetX / mentWidth; |
| 74 | + setCurIndex(page) |
| 75 | + } |
38 | 76 | return ( |
39 | | - <View style={StyleSheet.flatten([styles.box, { height }])}> |
40 | | - <View style={StyleSheet.flatten([styles.banner, { width }])}> |
41 | | - <View style={StyleSheet.flatten([styles.bannerOut, { marginLeft: curIndex * -100 + '%' }])}> |
42 | | - {dataSource.map((item: any, index: number) => { |
43 | | - return <Image style={styles.tinyLogo} key={index} source={{ |
44 | | - uri: item.url, |
45 | | - }} /> |
46 | | - })} |
47 | | - </View> |
48 | | - <View style={styles.dotBox}> |
49 | | - {dataSource.map((_: any, index: number) => { |
50 | | - return <View key={index} style={StyleSheet.flatten([styles.dot, index === curIndex ? styles.dotSetColor : styles.dotColor])} /> |
51 | | - })} |
52 | | - </View> |
| 77 | + <View style={StyleSheet.flatten([styles.banner, { width, height }])}> |
| 78 | + <ScrollView |
| 79 | + ref={scrollToRef} |
| 80 | + horizontal={true} |
| 81 | + showsHorizontalScrollIndicator={false} |
| 82 | + pagingEnabled={true} |
| 83 | + onScrollBeginDrag={onScrollBeginDrag} |
| 84 | + onScrollEndDrag={onScrollEndDrag} |
| 85 | + onMomentumScrollEnd={onMomentumScrollEnd} |
| 86 | + > |
| 87 | + {dataSource.map((item: dataSourceType, index: number) => { |
| 88 | + return ( |
| 89 | + <View key={index} style={{ width, height }}> |
| 90 | + <View style={{ padding: 12 }}> |
| 91 | + <TouchableOpacity activeOpacity={1} onPress={() => { |
| 92 | + item.onClick && item.onClick() |
| 93 | + }}> |
| 94 | + <Image |
| 95 | + key={index} |
| 96 | + style={StyleSheet.flatten([{ borderRadius, width: "100%", height: "100%", }])} |
| 97 | + resizeMode="cover" |
| 98 | + source={typeof item.url === "number" ? item.url : { uri: item.url, }} |
| 99 | + /> |
| 100 | + </TouchableOpacity> |
| 101 | + </View> |
| 102 | + </View> |
| 103 | + ) |
| 104 | + })} |
| 105 | + </ScrollView> |
| 106 | + <View style={styles.dotBox}> |
| 107 | + {dataSource.map((_: dataSourceType, index: number) => { |
| 108 | + return <View |
| 109 | + key={index} |
| 110 | + style={ |
| 111 | + StyleSheet.flatten([dotStyle === "block" ? styles.block : styles.dot, |
| 112 | + index === curIndex ? styles.dotSetColor : styles.dotColor]) |
| 113 | + } /> |
| 114 | + })} |
53 | 115 | </View> |
54 | 116 | </View> |
55 | | - |
56 | 117 | ) |
57 | 118 | } |
58 | 119 | const styles = StyleSheet.create({ |
59 | | - box: { |
60 | | - margin: 12, |
61 | | - height: 130, |
62 | | - }, |
63 | 120 | banner: { |
64 | 121 | width: "100%", |
65 | | - borderRadius: 25, |
66 | 122 | position: "relative", |
67 | 123 | overflow: "hidden", |
68 | 124 | }, |
69 | | - bannerOut: { |
70 | | - width: "100%", |
71 | | - height: "100%", |
72 | | - flexDirection: 'row', |
73 | | - alignItems: 'center', |
74 | | - marginLeft: 0 |
75 | | - }, |
76 | | - tinyLogo: { |
77 | | - width: "100%", |
78 | | - height: "100%", |
79 | | - }, |
80 | 125 | dotBox: { |
81 | 126 | width: "100%", |
82 | 127 | flexDirection: 'row', |
83 | 128 | alignItems: 'center', |
84 | 129 | justifyContent: "center", |
85 | 130 | position: "absolute", |
86 | | - bottom: 12 |
| 131 | + bottom: 22 |
87 | 132 | }, |
88 | 133 | dot: { |
89 | | - width: 10, |
90 | | - height: 10, |
91 | | - borderRadius: 5, |
| 134 | + width: 8, |
| 135 | + height: 8, |
| 136 | + borderRadius: 4, |
| 137 | + marginTop: 0, |
| 138 | + marginBottom: 0, |
| 139 | + marginLeft: 8, |
| 140 | + marginRight: 8 |
| 141 | + }, |
| 142 | + block: { |
| 143 | + width: 16, |
| 144 | + height: 3, |
92 | 145 | marginTop: 0, |
93 | 146 | marginBottom: 0, |
94 | | - marginLeft: 12, |
95 | | - marginRight: 12 |
| 147 | + marginLeft: 8, |
| 148 | + marginRight: 8 |
96 | 149 | }, |
97 | 150 | dotColor: { |
98 | 151 | backgroundColor: "lightgray", |
|
0 commit comments