-
Notifications
You must be signed in to change notification settings - Fork 42
/
ImageZoom.tsx
84 lines (78 loc) 路 1.86 KB
/
ImageZoom.tsx
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
76
77
78
79
80
81
82
83
84
import React, { forwardRef, ForwardRefRenderFunction } from 'react';
import { StyleSheet } from 'react-native';
import { GestureDetector } from 'react-native-gesture-handler';
import Animated from 'react-native-reanimated';
import { useGestures } from '../hooks/useGestures';
import { useImageLayout } from '../hooks/useImageLayout';
import { useImageZoomHandle } from '../hooks/useImageZoomHandle';
import type { ImageZoomProps, ImageZoomRef } from '../types';
const styles = StyleSheet.create({
image: {
flex: 1,
},
});
const ImageZoom: ForwardRefRenderFunction<ImageZoomRef, ImageZoomProps> = (
{
uri = '',
minScale,
maxScale,
doubleTapScale,
minPanPointers,
maxPanPointers,
isPanEnabled,
isPinchEnabled,
isSingleTapEnabled,
isDoubleTapEnabled,
onInteractionStart,
onInteractionEnd,
onPinchStart,
onPinchEnd,
onPanStart,
onPanEnd,
onSingleTap,
onDoubleTap,
onResetAnimationEnd,
onLayout,
style = {},
...props
},
ref
) => {
const { width, height, center, onImageLayout } = useImageLayout({ onLayout });
const { animatedStyle, gestures, reset } = useGestures({
width,
height,
center,
minScale,
maxScale,
doubleTapScale,
minPanPointers,
maxPanPointers,
isPanEnabled,
isPinchEnabled,
isSingleTapEnabled,
isDoubleTapEnabled,
onInteractionStart,
onInteractionEnd,
onPinchStart,
onPinchEnd,
onPanStart,
onPanEnd,
onSingleTap,
onDoubleTap,
onResetAnimationEnd,
});
useImageZoomHandle(ref, reset);
return (
<GestureDetector gesture={gestures}>
<Animated.Image
style={[styles.image, style, animatedStyle]}
source={{ uri }}
resizeMode="contain"
onLayout={onImageLayout}
{...props}
/>
</GestureDetector>
);
};
export default forwardRef(ImageZoom);