Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Add initialScale property #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default class App extends React.Component {
| enableCenterFocus | boolean | for disabling focus on image center if user doesn't want it | true |
| onSwipeDown | () => void | function that fires when user swipes down | null |
| swipeDownThreshold | number | threshold for firing swipe down function | 230 |
| initialScale | number | initial zoom | 1 |
| minScale | number | minimum zoom scale | 0.6 |
| maxScale | number | maximum zoom scale | 10 |
| useNativeDriver | boolean | Whether to animate using [`useNativeDriver`](https://reactnative.dev/docs/animations#using-the-native-driver) | false |
Expand Down
25 changes: 13 additions & 12 deletions src/image-zoom/image-zoom.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
private animatedPositionY = new Animated.Value(0);

// 缩放大小
private scale = 1;
private animatedScale = new Animated.Value(1);
private initialScale = this.props.initialScale;
private scale = this.initialScale;
private animatedScale = new Animated.Value(this.initialScale);
private zoomLastDistance: number | null = null;
private zoomCurrentDistance = 0;

Expand Down Expand Up @@ -132,20 +133,20 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
this.isDoubleClick = true;

if (this.props.enableDoubleClickZoom) {
if (this.scale > 1 || this.scale < 1) {
if (this.scale !== this.initialScale) {
// 回归原位
this.scale = 1;
this.scale = this.initialScale;

this.positionX = 0;
this.positionY = 0;
} else {
// 开始在位移地点缩放
// 记录之前缩放比例
// 此时 this.scale 一定为 1
// 此时 this.scale 一定为 `this.initialScale`
const beforeScale = this.scale;

// 开始缩放
this.scale = 2;
this.scale *= 2;

// 缩放 diff
const diffScale = this.scale - beforeScale;
Expand Down Expand Up @@ -461,8 +462,8 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
public resetScale = (): void => {
this.positionX = 0;
this.positionY = 0;
this.scale = 1;
this.animatedScale.setValue(1);
this.scale = this.initialScale;
this.animatedScale.setValue(this.initialScale);
};

public panResponderReleaseResolve = (): void => {
Expand All @@ -477,9 +478,9 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
}
}

if (this.props.enableCenterFocus && this.scale < 1) {
// 如果缩放小于1,强制重置为 1
this.scale = 1;
if (this.props.enableCenterFocus && this.scale < this.initialScale) {
// 如果缩放小于`this.initialScale`,强制重置为 `this.initialScale`
this.scale = this.initialScale;
Animated.timing(this.animatedScale, {
toValue: this.scale,
duration: 100,
Expand Down Expand Up @@ -540,7 +541,7 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
}

// 拖拽正常结束后,如果没有缩放,直接回到0,0点
if (this.props.enableCenterFocus && this.scale === 1) {
if (this.props.enableCenterFocus && this.scale === this.initialScale) {
this.positionX = 0;
this.positionY = 0;
Animated.timing(this.animatedPositionX, {
Expand Down
5 changes: 5 additions & 0 deletions src/image-zoom/image-zoom.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export class ImageZoomProps {
*/
public useHardwareTextureAndroid?: boolean = true;

/**
* iniatial zoom scale
*/
public initialScale?: number = 1.0;

/**
* minimum zoom scale
*/
Expand Down