Cómo calcular y detectar PanResponder evento?

0

Pregunta

Soy bastante nuevo para la animación y el gesto de controlador. Estoy leyendo la documentación oficial reaccionar nativo de animaciones y me encontré con un fragmento de este código que le permite moverse alrededor de la caja azul.

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;

Sin embargo, quiero consola.registro o hacer sone cálculos cuando panresponder mover, así que he intentado modificar mi opPanResponderMove pero el cuadro azul no se mueve más. Me preguntaba ¿por qué?

Mi código modificado:

onPanResponderMove: (event, gestureState) => {
        console.log(event);
        console.log(gestureState);
        Animated.event([
            null,
            { dx: pan.x, dy: pan.y }
          ])
      },
animation react-native
2021-11-24 01:44:24
1

Mejor respuesta

1

Hubo un problema acerca de su problema aquí

Dijo que

La razón fue porque la Animación.evento() sólo genera la función que se utiliza desde onPanResponderMove, también las necesidades de los argumentos predeterminados evt, gestureState

y aquí está la solución:

onPanResponderMove: (event, gestureState) => {
        console.log(event);
        console.log(gestureState);
        return Animated.event([
            null,
            { dx: pan.x, dy: pan.y }
          ])(event,gestureState);
      },
2021-11-24 03:51:12

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................