反应使用手势和反应弹簧性能缓慢

dav*_*ler 2 javascript draggable reactjs react-spring react-use-gesture

文档中的拖动示例(例如此示例)速度非常快,并且非常准确地映射到我的手指位置。我尝试一对一复制示例,但与我的手指位置相比有明显的滞后。

这是一个代码沙箱示例,在真实设备上测试时有明显的滞后。

信息:

  • React 使用手势版本:(我已经尝试了各种版本组合,但没有影响,但这是codesandbox 中的配置)
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-scripts": "4.0.0",
    "react-spring": "9.1.2",
    "react-use-gesture": "9.1.3"
Run Code Online (Sandbox Code Playgroud)
  • 设备:iPhone 12 Pro Max
  • 操作系统:[例如iOS14.5.1]
  • 浏览器镀铬
function PullRelease() {
  const [{ x }, api] = useSpring(() => ({ x: 0 }));
  const bind = useDrag(({ movement: [mx], down }) =>
    api.start({ x: down ? mx : 0 })
  );
  return (
    <animated.div {...bind()} style={{ padding: 40, background: "gold", x }}>
      Hello World
    </animated.div>
  );
}
Run Code Online (Sandbox Code Playgroud)

dav*_*ler 5

我梳理了示例的源代码。事实上,问题出在反应弹簧上,我需要immediate: true在触摸处于活动状态时提供。

https://codesandbox.io/s/react-spring-gesture-basic-swipe-immediate-6bje9?file=/src/App.js

function PullRelease() {
  const [{ x }, api] = useSpring(() => ({ x: 0 }));
  const bind = useDrag(({ movement: [mx], down }) =>
    api.start({ x: down ? mx : 0, 
        immediate: down // the key line
    })
  );
  return (
    <animated.div {...bind()} style={{ padding: 40, background: "gold", x }}>
      Hello World
    </animated.div>
  );
}
Run Code Online (Sandbox Code Playgroud)