在React Native中旋转SVG

Geo*_*emp 3 react-native

我有一个在react native中创建的SVG,我只是想尽可能以最有效的方式连续旋转360度。

谢谢。

Ami*_*mit 11

只需使用变换即可。它工作完美。

<YourSvg
    height={20}
    width={20}
    style={{ transform: [{ rotateY: '180deg' }] }}
/>
Run Code Online (Sandbox Code Playgroud)

  • `style={{transform: [{rotate: '90deg'}]}}` 这对我有用 (2认同)

den*_*emm 7

只需将SVG包装在View组件中,然后使用Animated API。您的代码将如下所示:

class YourComponent extends React.Component {

  constructor(props) {
    super(props);
    this.animation = new Animated.Value(0);
  }

  render() {

    const rotation = this.animation.interpolate({
      inputRange: [0, 1],
      outputRange: ['0deg', '360deg']
    });

    return (
      <Animated.View
        style={{transform: [{rotate: rotation}] }}
      >
        <YourSVG />
      </Animated.View>
    );


  componentDidMount() {

    Animated.loop(
      Animated.timing(this.animation, {toValue: 1, duration: 2000})
    ).start();    
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果 YourSVG 有内部转换并且您只想旋转 SVG 的该部分怎么办?类似于 ``` &lt;Svg width={100} height={100}&gt; &lt;矩形宽度={'100%'} height={'100%'} fill={'blue'}/&gt; &lt;矩形宽度={ 20} height={20} x={20} y={60} fill={'red'}/&gt; &lt;/Svg&gt; ``` 要旋转红色矩形的位置。 (2认同)