无法使用react native在SVG中设置路径动画

mzsu5hc0  于 2023-03-19  发布在  React
关注(0)|答案(1)|浏览(122)

我尝试在SVG rotate中创建一个路径。我一直收到错误“undefined is not a function”。它似乎是由animatedProps的声明引起的。代码如下:

import React, { forwardRef, useState, useRef } from 'react';
import {View} from 'react-native';
import { Svg, Path } from 'react-native-svg';
import Animated from 'react-native-reanimated';

const AnimatedPath = Animated.createAnimatedComponent(Path);

const Shape = forwardRef((props, ref) => {
  const [currentPath, setCurrentPath] = useState([]);
  const rotation = useRef(new Animated.Value(0)).current;

  const startRotation = () => {
    Animated.loop(
      Animated.timing(rotation, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
      })
    ).start();
  };

  const animatedProps = Animated.useAnimatedProps(() => {
    const rotate = `rotate(${rotation.value * 360}, 0, 0)`;
    return { transform: [{ rotate }] }
  });

  return (
    <View style={styles.container}>
      <Svg style={styles.svg} height="100%" width="100%">
        <AnimatedPath
          d={currentPath.join('')}
          stroke={props.colour}
          fill={'transparent'}
          strokeWidth={2}
          strokeLinejoin={'round'}
          strokeLinecap={'round'}
          animatedProps={animatedProps}
          onPress={() => { }}
        />
      </Svg>
    </View>
  );
});

请注意,我已经删除了创建路径的代码,以使代码更一致。

fxnxkyjh

fxnxkyjh1#

Animated.loop不是“react-native-reanimated”的属性。计时也是如此。您应该使用“react-native-reanimated”中的withTiming和withRepeat API。
您正在混合“react-native”和“react-native-reanimated”的Animated属性。请尝试仅使用一个包。请从“react-native”或“react-native”reanimated“使用。

相关问题