简体   繁体   English

如何计算和检测 PanResponder 事件?

[英]How to calculate and detect PanResponder event?

I am pretty new to animation and gesture handler.我对 animation 和手势处理程序很陌生。 I am reading the documentation in the official react native animations and I found a snippet of this code that lets you move around the blue box.我正在阅读官方反应原生动画中的文档,我发现了这段代码的片段,它可以让你在蓝色框内移动。

https://reactnative.dev/docs/animations https://reactnative.dev/docs/animations

import React, { useRef } from "react";
import { Animated, View, StyleSheet, PanResponder, Text } from "react-native";

const App = () => {
  const pan = useRef(new Animated.ValueXY()).current;
  const panResponder = useRef(
    PanResponder.create({
      onMoveShouldSetPanResponder: () => true,
      onPanResponderMove: Animated.event([
        null,
        { dx: pan.x, dy: pan.y }
      ]),
      onPanResponderRelease: () => {
        Animated.spring(pan, { toValue: { x: 0, y: 0 } }).start();
      }
    })
  ).current;

  return (
    <View style={styles.container}>
      <Text style={styles.titleText}>Drag & Release this box!</Text>
      <Animated.View
        style={{
          transform: [{ translateX: pan.x }, { translateY: pan.y }]
        }}
        {...panResponder.panHandlers}
      >
        <View style={styles.box} />
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center"
  },
  titleText: {
    fontSize: 14,
    lineHeight: 24,
    fontWeight: "bold"
  },
  box: {
    height: 150,
    width: 150,
    backgroundColor: "blue",
    borderRadius: 5
  }
});

export default App;

However, I want to console.log or do sone calculations when panresponder move so I tried modifying my opPanResponderMove but the blue box doesnt move anymore.但是,我想在 panresponder 移动时进行 console.log 或进行计算,所以我尝试修改我的 opPanResponderMove 但蓝色框不再移动。 I was wondering why?我想知道为什么?

My modified code:我修改后的代码:

onPanResponderMove: (event, gestureState) => {
        console.log(event);
        console.log(gestureState);
        Animated.event([
            null,
            { dx: pan.x, dy: pan.y }
          ])
      },

There was an issue about your problem here这里有一个关于您的问题的问题

It said that它说

Reason was because Animation.event() only generates the function which is used from onPanResponderMove, it also needs the default arguments evt, gestureState原因是因为 Animation.event() 只生成从 onPanResponderMove 使用的 function,它还需要默认的 arguments evt,gestureState

and here is the fix:这是修复:

onPanResponderMove: (event, gestureState) => {
        console.log(event);
        console.log(gestureState);
        return Animated.event([
            null,
            { dx: pan.x, dy: pan.y }
          ])(event,gestureState);
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM