Skip to content

Commit e4770ec

Browse files
committed
Update demo
1 parent 9133c51 commit e4770ec

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

demo/components/ImageViewer/Image.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
PanResponderGestureState,
1313
Text,
1414
SafeAreaView,
15+
Platform,
1516
} from 'react-native';
1617
import {
1718
HandlerStateChangeEvent,
@@ -102,12 +103,13 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
102103
}
103104

104105
if (Math.abs(gesture.dx) > Math.abs(gesture.dy)) {
105-
this.props.toggleEnableScroll(true);
106-
106+
this._debug('_onPanResponderMove', 'horizontal');
107+
this.props.onZoomStateChange(false);
107108
return;
108109
}
109110

110-
this.props.toggleEnableScroll(false);
111+
this._debug('_onPanResponderMove', 'vertical');
112+
this.props.onZoomStateChange(true);
111113
this._translateXY.setValue({x: 0, y: Math.max(0, gesture.dy)});
112114
}
113115
private _onPanResponderEnd(_evt: any, gesture: PanResponderGestureState) {
@@ -145,7 +147,6 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
145147
}).start(() => this.props.onClose());
146148
} else {
147149
// fixed case swipe left when jump restart
148-
this.props.toggleEnableScroll(true);
149150
Animated.spring(this._translateXY, {
150151
toValue: {x: 0, y: 0},
151152
useNativeDriver: true,
@@ -176,7 +177,6 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
176177
]).start();
177178
this._lastScale = 1;
178179
this._lastOffset = {x: 0, y: 0};
179-
this.props.toggleEnableScroll(true);
180180
} else {
181181
const scale =
182182
this._getRatio() <= this._getMinimumScale() ? this._getMaximumScale() : SCREEN_WIDTH / this.state.width!;
@@ -232,7 +232,6 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
232232
});
233233
this._lastScale = scale;
234234
this._setIsZooming(true);
235-
this.props.toggleEnableScroll(false);
236235
}
237236
};
238237

@@ -265,7 +264,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
265264
let scale = evt.nativeEvent.scale as number;
266265
if (scale < this._getMinimumScale()) {
267266
scale = this._getMinimumScale();
268-
this.props.toggleEnableScroll(true);
267+
269268
this._setIsZooming(false);
270269
} else {
271270
scale = Math.min(this._getMaximumScale(), scale);
@@ -301,8 +300,6 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
301300
this._scale.setValue(Math.min(this._getMaximumScale(), scale));
302301
this._setIsZooming(true);
303302
}
304-
305-
this.props.toggleEnableScroll(false);
306303
}
307304
};
308305

@@ -441,7 +438,10 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
441438
return (
442439
<View style={styles.container}>
443440
<Animated.View style={backdropStyle} {...this._panResponder.panHandlers} />
444-
<Animated.View style={moveObjStyle} {...this._panResponder.panHandlers} renderToHardwareTextureAndroid>
441+
<Animated.View
442+
style={moveObjStyle}
443+
{...(this.state.isZooming || Platform.OS === 'ios' ? this._panResponder.panHandlers : {})}
444+
renderToHardwareTextureAndroid>
445445
<TapGestureHandler numberOfTaps={2} onActivated={this._handleImageZoomInOut}>
446446
<PinchGestureHandler
447447
onGestureEvent={this._onPinchGestureEvent}

demo/components/ImageViewer/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import {Modal, Dimensions, VirtualizedList, Platform} from 'react-native';
2+
import {Modal, Dimensions, VirtualizedList, Platform, VirtualizedListProps} from 'react-native';
33
import Image from './Image';
44
import {ImageViewerImageProps, ImageViewerComponentProps, ImageViewerComponentState} from './types';
55

@@ -14,15 +14,14 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
1414
animationType: 'none',
1515
};
1616

17-
private _scrollRef: {current: any} = React.createRef();
17+
private _scrollRef: {current: any} = React.createRef<VirtualizedList<any>>();
1818

1919
private _closeInternal = () => this.props.onClose();
2020
private _renderImage = (info: {item: ImageViewerImageProps; index: number}) => (
2121
<Image
2222
source={info.item.source}
2323
title={info.item.title}
2424
onClose={this._closeInternal}
25-
toggleEnableScroll={this._handleToggleScrollState}
2625
onZoomStateChange={this._onZoomStateChange}
2726
imageIndex={info.index}
2827
imagesTotal={this._getItemCount()}
@@ -34,9 +33,10 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
3433
/>
3534
);
3635

37-
private _onZoomStateChange = (isZooming: boolean) => this.setState({isZooming});
38-
private _handleToggleScrollState = (enabled: boolean) => {
39-
this.setState({scrollEnabled: enabled});
36+
private _onZoomStateChange = (isZooming: boolean) => {
37+
this._scrollRef.current.getScrollRef().setNativeProps({
38+
scrollEnabled: !isZooming,
39+
});
4040
};
4141

4242
private _getItemCount = () => this.props.images.length;
@@ -75,7 +75,7 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
7575
getItemCount={this._getItemCount}
7676
getItem={this._getItem}
7777
getItemLayout={this._getItemLayout}
78-
scrollEnabled={!this.state.isZooming}
78+
scrollEnabled
7979
ref={this._scrollRef}
8080
removeClippedSubviews={true}
8181
maxToRenderPerBatch={2}
@@ -84,7 +84,6 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
8484
pagingEnabled
8585
initialScrollIndex={this.props.initialIndex}
8686
listKey={'RNImageViewer'}
87-
disableScrollViewPanResponder={!this.state.scrollEnabled}
8887
{...platformProps}
8988
/>
9089
</Modal>

demo/components/ImageViewer/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export interface ImageComponentProps extends ImageComponentOptionalProps {
1111
source: ImageURISource | ImageRequireSource;
1212
title?: string;
1313
onClose: () => void;
14-
toggleEnableScroll: (enabled: boolean) => void;
1514
onZoomStateChange: (isZooming: boolean) => void;
1615

1716
imageIndex: number;

0 commit comments

Comments
 (0)