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

Add threshold to double click location #148

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
12 changes: 11 additions & 1 deletion src/image-zoom/image-zoom.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo

// 上一次点击的时间
private lastClickTime = 0;
private lastClickLocation = { x: 0, y: 0 };

// 双击时的位置
private doubleClickX = 0;
Expand Down Expand Up @@ -107,8 +108,16 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
}, this.props.longPressTime);

if (evt.nativeEvent.changedTouches.length <= 1) {
const clickLocation = {
x: evt.nativeEvent.changedTouches[0].pageX,
y: evt.nativeEvent.changedTouches[0].pageY,
};
// 一个手指的情况
if (new Date().getTime() - this.lastClickTime < (this.props.doubleClickInterval || 0)) {
if (
new Date().getTime() - this.lastClickTime < (this.props.doubleClickInterval || 0) &&
Math.abs(clickLocation.x - this.lastClickLocation.x) < (this.props.doubleClickLocationThreshold || 30) &&
Math.abs(clickLocation.y - this.lastClickLocation.y) < (this.props.doubleClickLocationThreshold || 30)
) {
// 认为触发了双击
this.lastClickTime = 0;

Expand Down Expand Up @@ -178,6 +187,7 @@ export default class ImageViewer extends React.Component<ImageZoomProps, ImageZo
}
} else {
this.lastClickTime = new Date().getTime();
this.lastClickLocation = clickLocation;
}
}
},
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 @@ -78,6 +78,11 @@ export class ImageZoomProps {
*/
public doubleClickInterval?: number = 175;

/**
* Click location threshold to consider consecutive two clicks
* as double click used to avoid double clicking in different locations
*/
public doubleClickLocationThreshold?: number = 30;
/**
* If provided this will cause the view to zoom and pan to the center point
* Duration is optional and defaults to 300 ms.
Expand Down