|
@@ -25,9 +25,13 @@ type PhotoViewState = {
|
|
// 触摸开始时 y 原始坐标
|
|
// 触摸开始时 y 原始坐标
|
|
pageY: number;
|
|
pageY: number;
|
|
// 触摸开始时图片 x 偏移量
|
|
// 触摸开始时图片 x 偏移量
|
|
- offsetX: number;
|
|
|
|
|
|
+ lastX: number;
|
|
// 触摸开始时图片 y 偏移量
|
|
// 触摸开始时图片 y 偏移量
|
|
- offsetY: number;
|
|
|
|
|
|
+ lastY: number;
|
|
|
|
+ // 上一次触摸开始时 x 原始坐标
|
|
|
|
+ lastPageX: number | undefined;
|
|
|
|
+ // 上一次触摸开始时 y 原始坐标
|
|
|
|
+ lastPageY: number | undefined;
|
|
// 触摸开始时时间
|
|
// 触摸开始时时间
|
|
touchedTime: number;
|
|
touchedTime: number;
|
|
} & animationType;
|
|
} & animationType;
|
|
@@ -46,8 +50,12 @@ export default class PhotoView extends React.Component<
|
|
|
|
|
|
pageX: 0,
|
|
pageX: 0,
|
|
pageY: 0,
|
|
pageY: 0,
|
|
- offsetX: 0,
|
|
|
|
- offsetY: 0,
|
|
|
|
|
|
+
|
|
|
|
+ lastX: 0,
|
|
|
|
+ lastY: 0,
|
|
|
|
+
|
|
|
|
+ lastPageX: undefined,
|
|
|
|
+ lastPageY: undefined,
|
|
touchedTime: 0,
|
|
touchedTime: 0,
|
|
|
|
|
|
animation: defaultAnimationConfig,
|
|
animation: defaultAnimationConfig,
|
|
@@ -80,18 +88,18 @@ export default class PhotoView extends React.Component<
|
|
touched: true,
|
|
touched: true,
|
|
pageX,
|
|
pageX,
|
|
pageY,
|
|
pageY,
|
|
- offsetX: prevState.x,
|
|
|
|
- offsetY: prevState.y,
|
|
|
|
|
|
+ lastX: prevState.x,
|
|
|
|
+ lastY: prevState.y,
|
|
touchedTime: Date.now(),
|
|
touchedTime: Date.now(),
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
|
|
|
|
handleMove = (newPageX, newPageY) => {
|
|
handleMove = (newPageX, newPageY) => {
|
|
if (this.state.touched) {
|
|
if (this.state.touched) {
|
|
- this.setState(({ pageX, pageY, offsetX, offsetY }) => {
|
|
|
|
|
|
+ this.setState(({ pageX, pageY, lastX, lastY }) => {
|
|
return {
|
|
return {
|
|
- x: newPageX - pageX + offsetX,
|
|
|
|
- y: newPageY - pageY + offsetY,
|
|
|
|
|
|
+ x: newPageX - pageX + lastX,
|
|
|
|
+ y: newPageY - pageY + lastY,
|
|
};
|
|
};
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -99,31 +107,40 @@ export default class PhotoView extends React.Component<
|
|
|
|
|
|
handleDoubleClick = (e) => {
|
|
handleDoubleClick = (e) => {
|
|
const { pageX, pageY } = e;
|
|
const { pageX, pageY } = e;
|
|
- this.setState(({ x, y, scale }) => {
|
|
|
|
- const toScale = scale > 1 ? 1 : 4;
|
|
|
|
- const { distanceX, distanceY } = getPositionOnScale({ x, y, pageX, pageY, toScale });
|
|
|
|
|
|
+ this.setState(({ x, y, scale, lastPageX, lastPageY }) => {
|
|
return {
|
|
return {
|
|
- x: distanceX,
|
|
|
|
- y: distanceY,
|
|
|
|
pageX,
|
|
pageX,
|
|
pageY,
|
|
pageY,
|
|
- scale: toScale,
|
|
|
|
|
|
+ ...getPositionOnScale({
|
|
|
|
+ x,
|
|
|
|
+ y,
|
|
|
|
+ pageX,
|
|
|
|
+ pageY,
|
|
|
|
+ lastPageX,
|
|
|
|
+ lastPageY,
|
|
|
|
+ fromScale: scale,
|
|
|
|
+ toScale: scale > 1 ? 1 : 2,
|
|
|
|
+ }),
|
|
};
|
|
};
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
handleWheel = (e) => {
|
|
handleWheel = (e) => {
|
|
const { pageX, pageY, deltaY } = e;
|
|
const { pageX, pageY, deltaY } = e;
|
|
- this.setState(({ x, y, scale }) => {
|
|
|
|
- const toScale = scale + deltaY / 100;
|
|
|
|
- const { distanceX, distanceY } = getPositionOnScale({ x, y, pageX, pageY, toScale });
|
|
|
|
-
|
|
|
|
|
|
+ this.setState(({ x, y, scale, lastPageX, lastPageY }) => {
|
|
return {
|
|
return {
|
|
- x: distanceX,
|
|
|
|
- y: distanceY,
|
|
|
|
pageX,
|
|
pageX,
|
|
pageY,
|
|
pageY,
|
|
- scale: toScale,
|
|
|
|
|
|
+ ...getPositionOnScale({
|
|
|
|
+ x,
|
|
|
|
+ y,
|
|
|
|
+ pageX,
|
|
|
|
+ pageY,
|
|
|
|
+ lastPageX,
|
|
|
|
+ lastPageY,
|
|
|
|
+ fromScale: scale,
|
|
|
|
+ toScale: scale - deltaY / 100 / 2,
|
|
|
|
+ }),
|
|
animation: defaultAnimationConfig,
|
|
animation: defaultAnimationConfig,
|
|
};
|
|
};
|
|
});
|
|
});
|
|
@@ -153,20 +170,20 @@ export default class PhotoView extends React.Component<
|
|
this.setState(({
|
|
this.setState(({
|
|
x,
|
|
x,
|
|
y,
|
|
y,
|
|
- offsetX,
|
|
|
|
- offsetY,
|
|
|
|
|
|
+ lastX,
|
|
|
|
+ lastY,
|
|
scale,
|
|
scale,
|
|
touchedTime,
|
|
touchedTime,
|
|
...restPrevState
|
|
...restPrevState
|
|
}) => {
|
|
}) => {
|
|
- const hasMove: boolean = pageX !== restPrevState.pageX || pageY !== restPrevState.pageY;
|
|
|
|
|
|
+ const hasMove = pageX !== restPrevState.pageX || pageY !== restPrevState.pageY;
|
|
return {
|
|
return {
|
|
touched: false,
|
|
touched: false,
|
|
...jumpToSuitableOffset({
|
|
...jumpToSuitableOffset({
|
|
x,
|
|
x,
|
|
y,
|
|
y,
|
|
- offsetX,
|
|
|
|
- offsetY,
|
|
|
|
|
|
+ lastX,
|
|
|
|
+ lastY,
|
|
width,
|
|
width,
|
|
height,
|
|
height,
|
|
scale,
|
|
scale,
|