Skip to content

Commit ccac2db

Browse files
committed
Update demo
1 parent 88b2467 commit ccac2db

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

demo/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import {Colors} from 'react-native/Libraries/NewAppScreen';
2424
import axios from 'axios';
2525
import ImageViewer from './components/ImageViewer';
26+
import {GestureHandlerRootView} from 'react-native-gesture-handler';
2627

2728
const WIDTH = Dimensions.get('window').width;
2829

demo/android/app/src/main/java/com/demo/MainActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.demo;
22

33
import com.facebook.react.ReactActivity;
4+
import com.facebook.react.ReactActivityDelegate;
5+
import com.facebook.react.ReactRootView;
6+
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
47

58
public class MainActivity extends ReactActivity {
69

@@ -12,4 +15,14 @@ public class MainActivity extends ReactActivity {
1215
protected String getMainComponentName() {
1316
return "demo";
1417
}
18+
19+
@Override
20+
protected ReactActivityDelegate createReactActivityDelegate() {
21+
return new ReactActivityDelegate(this, getMainComponentName()) {
22+
@Override
23+
protected ReactRootView createRootView() {
24+
return new RNGestureHandlerEnabledRootView(MainActivity.this);
25+
}
26+
};
27+
}
1528
}

demo/components/ImageViewer/Image.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
5151
width: null,
5252
height: null,
5353
loading: true,
54+
isZooming: false,
5455
} as ImageComponentState;
5556

5657
this._translateXY = new Animated.ValueXY();
@@ -103,7 +104,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
103104
if (Math.abs(gesture.dx) > Math.abs(gesture.dy)) {
104105
this.props.toggleEnableScroll(true);
105106

106-
return false;
107+
return;
107108
}
108109

109110
this.props.toggleEnableScroll(false);
@@ -160,6 +161,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
160161

161162
if (this._lastScale > this._getMinimumScale()) {
162163
this._translateXY.setOffset({x: 0, y: 0});
164+
this._setIsZooming(false);
163165
Animated.parallel([
164166
Animated.timing(this._translateXY, {
165167
toValue: {x: 0, y: 0},
@@ -229,6 +231,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
229231
this._lastOffset = {x, y};
230232
});
231233
this._lastScale = scale;
234+
this._setIsZooming(true);
232235
this.props.toggleEnableScroll(false);
233236
}
234237
};
@@ -263,8 +266,10 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
263266
if (scale < this._getMinimumScale()) {
264267
scale = this._getMinimumScale();
265268
this.props.toggleEnableScroll(true);
269+
this._setIsZooming(false);
266270
} else {
267271
scale = Math.min(this._getMaximumScale(), scale);
272+
this._setIsZooming(true);
268273
}
269274

270275
this._lastScale = scale;
@@ -291,17 +296,24 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
291296

292297
if (scale < this._getMinimumScale()) {
293298
this._scale.setValue(this._getMinimumScale());
299+
this._setIsZooming(false);
294300
} else {
295301
this._scale.setValue(Math.min(this._getMaximumScale(), scale));
302+
this._setIsZooming(true);
296303
}
297304

298305
this.props.toggleEnableScroll(false);
299306
}
300307
};
301308

302309
private _debug = (...args: any[]) => this.props.debug && console.log(...args);
310+
private _setIsZooming = (isZooming: boolean) => this.setState({isZooming}, () => this.props.onZoomStateChange(isZooming));
303311

304312
private _renderHeader = () => {
313+
if (this.state.isZooming) {
314+
return null;
315+
}
316+
305317
const headerAnim: any = [
306318
styles.header,
307319
{
@@ -333,6 +345,10 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
333345
return null;
334346
}
335347

348+
if (this.state.isZooming) {
349+
return null;
350+
}
351+
336352
let innerComponent;
337353
if (renderFooter !== undefined) {
338354
if (typeof renderFooter !== 'function') {
@@ -425,7 +441,7 @@ class Image extends React.Component<ImageComponentProps, ImageComponentState> {
425441
return (
426442
<View style={styles.container}>
427443
<Animated.View style={backdropStyle} {...this._panResponder.panHandlers} />
428-
<Animated.View style={moveObjStyle} {...this._panResponder.panHandlers}>
444+
<Animated.View style={moveObjStyle} {...this._panResponder.panHandlers} renderToHardwareTextureAndroid>
429445
<TapGestureHandler numberOfTaps={2} onActivated={this._handleImageZoomInOut}>
430446
<PinchGestureHandler
431447
onGestureEvent={this._onPinchGestureEvent}

demo/components/ImageViewer/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const SCREEN_WIDTH = Dimensions.get('window').width;
88
class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewerComponentState> {
99
state: ImageViewerComponentState = {
1010
scrollEnabled: true,
11+
isZooming: false,
1112
};
1213
static defaultProps = {
1314
animationType: 'none',
@@ -22,6 +23,7 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
2223
title={info.item.title}
2324
onClose={this._closeInternal}
2425
toggleEnableScroll={this._handleToggleScrollState}
26+
onZoomStateChange={this._onZoomStateChange}
2527
imageIndex={info.index}
2628
imagesTotal={this._getItemCount()}
2729
// extendable props
@@ -32,6 +34,7 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
3234
/>
3335
);
3436

37+
private _onZoomStateChange = (isZooming: boolean) => this.setState({isZooming});
3538
private _handleToggleScrollState = (enabled: boolean) => {
3639
this.setState({scrollEnabled: enabled});
3740
};
@@ -72,7 +75,7 @@ class ImageViewer extends React.Component<ImageViewerComponentProps, ImageViewer
7275
getItemCount={this._getItemCount}
7376
getItem={this._getItem}
7477
getItemLayout={this._getItemLayout}
75-
scrollEnabled
78+
scrollEnabled={!this.state.isZooming}
7679
ref={this._scrollRef}
7780
removeClippedSubviews={true}
7881
maxToRenderPerBatch={2}

demo/components/ImageViewer/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ImageComponentProps extends ImageComponentOptionalProps {
1212
title?: string;
1313
onClose: () => void;
1414
toggleEnableScroll: (enabled: boolean) => void;
15+
onZoomStateChange: (isZooming: boolean) => void;
1516

1617
imageIndex: number;
1718
imagesTotal: number;
@@ -31,6 +32,7 @@ export interface ImageComponentState {
3132
width: number | null;
3233
height: number | null;
3334
loading: boolean;
35+
isZooming: boolean;
3436
}
3537

3638
// index.tsx
@@ -45,4 +47,5 @@ export interface ImageViewerComponentProps {
4547
}
4648
export interface ImageViewerComponentState {
4749
scrollEnabled: boolean;
50+
isZooming: boolean;
4851
}

0 commit comments

Comments
 (0)