forked from RouteAble/routeable-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageZoomScreen.js
75 lines (68 loc) · 2.21 KB
/
ImageZoomScreen.js
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
import React, { useState, useRef } from 'react';
import { View, Image, FlatList, Dimensions } from 'react-native';
import { PanGestureHandler, State } from 'react-native-gesture-handler';
const { width } = Dimensions.get('window');
function ImageZoomScreen({ route, navigation }) {
const { images, index } = route.params;
const [currentIndex, setCurrentIndex] = useState(index);
const flatListRef = useRef(null);
const handlePrevious = () => {
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1);
}
};
const handleNext = () => {
if (currentIndex < images.length - 1) {
setCurrentIndex(currentIndex + 1);
}
};
const onGestureEvent = ({ nativeEvent }) => {
if (nativeEvent.state === State.END) {
const { velocityX } = nativeEvent;
if (velocityX > 0) {
handlePrevious();
} else if (velocityX < 0) {
handleNext();
}
}
};
return (
<View style={{ flex: 1 }}>
<PanGestureHandler
onGestureEvent={onGestureEvent}
activeOffsetX={[-20, 20]}
failOffsetY={[-20, 20]}
>
<View style={{ flex: 1 }}>
<FlatList
ref={flatListRef}
data={images}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<View style={{ width, justifyContent: 'center', alignItems: 'center' }}>
<Image
source={{ uri: item }}
style={{ width: '80%', height: '80%', resizeMode: 'contain' }}
/>
</View>
)}
initialScrollIndex={index}
getItemLayout={(data, index) => ({ length: width, offset: width * index, index })}
onScrollToIndexFailed={(info) => {
const wait = new Promise((resolve) => {
setTimeout(resolve, 500);
});
wait.then(() => {
flatListRef.current?.scrollToIndex({ index: info.index, animated: true });
});
}}
/>
</View>
</PanGestureHandler>
</View>
);
}
export default ImageZoomScreen;