From 012226ba2006fee1e5226827c326f1227fc255f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristijan=20Tomi=C4=87?= Date: Tue, 2 Mar 2021 12:18:18 +0100 Subject: [PATCH 1/2] Update ReactNativeZoomableView.js handles double tap to zoom to center of the the view --- src/ReactNativeZoomableView.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ReactNativeZoomableView.js b/src/ReactNativeZoomableView.js index 6387013..c823d72 100644 --- a/src/ReactNativeZoomableView.js +++ b/src/ReactNativeZoomableView.js @@ -515,8 +515,8 @@ class ReactNativeZoomableView extends Component { const nextZoomStep = this._getNextZoomStep(); this._zoomToLocation( - e.nativeEvent.locationX, - e.nativeEvent.locationY, + 0, + 0, nextZoomStep, true ); @@ -567,6 +567,13 @@ class ReactNativeZoomableView extends Component { const currentElementWidth = originalWidth; const currentElementHeight = originalHeight; + if (x === 0 && y === 0) { + return { + x: 0, + y: 0, + } + } + const returnObj = { x: (-x + (currentElementWidth / 2)), y: (-y + (currentElementHeight / 2)), From b30217a5226429053d552709593373f0953d0e1a Mon Sep 17 00:00:00 2001 From: Simon Auer Date: Thu, 10 Jun 2021 08:43:53 +0200 Subject: [PATCH 2/2] added a new prop doubleTapZoomToCenter to allow zooming only to center --- README.md | 1 + src/ReactNativeZoomableView.js | 26 ++++++++++++++++++++------ typings/index.d.ts | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f77ebe8..2b9febe 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ These options can be used to limit and change the zoom behavior. | maxZoom | number | Maximum possible zoom level (zoom in). Can be set to `null` to allow unlimited zooming | 1.5 | | minZoom | number | Minimum possible zoom level (zoom out) | 0.5 | | doubleTapDelay | number | How much delay will still be recognized as double press (ms) | 300 | +| doubleTapZoomToCenter | boolean | If true, double tapping will always zoom to center of View instead of the direction it was double tapped in | bindToBorders | boolean | If true, it makes sure the object stays within box borders | true | | zoomStep | number | How much zoom should be applied on double tap | 0.5 | | pinchToZoomInSensitivity | number | the level of resistance (sensitivity) to zoom in (0 - 10) - higher is less sensitive | 3 | diff --git a/src/ReactNativeZoomableView.js b/src/ReactNativeZoomableView.js index c823d72..3f0733d 100644 --- a/src/ReactNativeZoomableView.js +++ b/src/ReactNativeZoomableView.js @@ -503,26 +503,40 @@ class ReactNativeZoomableView extends Component { * @private */ _handleDoubleTap(e, gestureState) { + const { onDoubleTapBefore, onDoubleTapAfter, doubleTapZoomToCenter } = this.props; + // ignore more than 2 touches if (gestureState.numberActiveTouches > 1 || !this.props.zoomEnabled) { return; } - if (this.props.onDoubleTapBefore) { - this.props.onDoubleTapBefore(e, gestureState, this._getZoomableViewEventObject()); + if (onDoubleTapBefore) { + onDoubleTapBefore(e, gestureState, this._getZoomableViewEventObject()); } const nextZoomStep = this._getNextZoomStep(); + // define new zoom position coordinates + const zoomPositionCoordinates = { + x: e.nativeEvent.locationX, + y: e.nativeEvent.locationY, + }; + + // if doubleTapZoomToCenter enabled -> always zoom to center instead + if (doubleTapZoomToCenter) { + zoomPositionCoordinates.x = 0; + zoomPositionCoordinates.y = 0; + } + this._zoomToLocation( - 0, - 0, + zoomPositionCoordinates.x, + zoomPositionCoordinates.y, nextZoomStep, true ); - if (this.props.onDoubleTapAfter) { - this.props.onDoubleTapAfter(e, gestureState, this._getZoomableViewEventObject({ + if (onDoubleTapAfter) { + onDoubleTapAfter(e, gestureState, this._getZoomableViewEventObject({ zoomLevel: nextZoomStep, })); } diff --git a/typings/index.d.ts b/typings/index.d.ts index 3fd8338..271c28a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -31,6 +31,7 @@ declare module '@dudigital/react-native-zoomable-view' { maxZoom?: number; minZoom?: number; doubleTapDelay?: number; + doubleTapZoomToCenter?: boolean; bindToBorders?: boolean; zoomStep?: number; pinchToZoomInSensitivity?: number;