javascript - 为什么我的 PanResponder 没有读取更新状态?
问题描述
这是我的代码:
const processPinch = (x1: number, y1: number, x2: number, y2: number) => {
function calcDistance(x1: number, y1: number, x2: number, y2: number) {
const dx = x1 - x2;
const dy = y1 - y2;
return Math.sqrt(dx * dx + dy * dy);
}
function calcCenter(x1: number, y1: number, x2: number, y2: number) {
function middle(p1: number, p2: number) {
return (p1 + p2) / 2;
}
return {
x: middle(x1, x2),
y: middle(y1, y2),
};
}
const distance = calcDistance(x1, y1, x2, y2);
const {x, y} = calcCenter(x1, y1, x2, y2);
if (!isZooming) {
console.log('if1');
setIsZooming(true);
setInitialX(x);
setInitialY(y);
setInitialTop(top);
setInitialLeft(left);
setInitialZoom(zoom);
setInitialDistance(distance);
} else {
console.log('if2');
const touchZoom = distance / initialDistance;
const dx = x - initialX;
const dy = y - initialY;
const left = (initialLeft + dx - x) * touchZoom + x;
const top = (initialTop + dy - y) * touchZoom + y;
const zoom = initialZoom * touchZoom;
const nextState = {
zoom,
left,
top,
};
const result = constrainExtent({
zoom,
left,
top,
});
}
};
const panResponder = React.useRef(
PanResponder.create({
onPanResponderGrant: (evt, gestureState) => {
// The gesture has started. Show visual feedback so the user knows
// what is happening!
// gestureState.d{x,y} will be set to zero now
},
onPanResponderTerminate: (evt, gestureState) => {
// Another component has become the responder, so this gesture
// should be cancelled
},
onShouldBlockNativeResponder: (evt, gestureState) => {
// Returns whether this component should block native components from becoming the JS
// responder. Returns true by default. Is currently only supported on android.
return true;
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onMoveShouldSetPanResponder: shouldRespond,
onStartShouldSetPanResponder: shouldRespond,
onMoveShouldSetPanResponderCapture: shouldRespond,
onStartShouldSetPanResponderCapture: shouldRespond,
onPanResponderMove: ({nativeEvent: {touches}, preventDefault}) => {
const {length} = touches;
if (length === 1) {
// console.log('if1');
// const [{pageX, pageY}] = touches;
// processTouch(pageX, pageY);
} else if (length === 2) {
const [touch1, touch2] = touches;
processPinch(touch1.pageX, touch1.pageY, touch2.pageX, touch2.pageY);
} else {
return;
}
// if (preventDefault) preventDefault();
},
onPanResponderRelease: () => {
setIsZooming(false);
setIsMoving(false);
},
}),
).current;
我正在处理processPinch
函数,但它没有继续执行 else 子句?我在考虑第一次运行时,它应该setZooming
是真的:
if (!isZooming) {
console.log('if1');
setIsZooming(true);
但processPinch
总是以如果?你在第一次运行时看到用户捏isZooming
是假的,在第二次运行时等等,isZooming
应该是真的,但它总是假的?
我在想这是因为useRef
和.current
?这是我在文档上看到的,所以我就是这样做的,我怎样才能使它与useRef
+一起工作.current
?
解决方案
推荐阅读
- java - 如何通过按键盘上的“回车”而不是按“开始比赛”按钮来开始赛事。”
- python - 在相对较大的 NumPy 数组中迭代替换值的更快方法
- c++ - 菜单暂停 C++ UE4
- python - 如何在 imwrite() 中使用特殊字符
- css - CSS Flexbox:在较小的屏幕上重叠弹性项目的问题
- visio - Visio 绘图资源管理器反向选择?
- redirect - 有没有办法知道哪些外部 URL 正在重定向到我网站上的页面?
- kubernetes - GKE 自动缩放器在无限循环中覆盖我的 HorizontalPodAutoscaler
- powershell - 如何在powershell中保存功能?
- r - 从 ggplot2 转换的 Plotly 绘图无法正确渲染