在 React Native 中旋转 SVG [英] Rotate an SVG in React Native

查看:52
本文介绍了在 React Native 中旋转 SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 React Native 中创建的 SVG,我只想以最有效的方式连续旋转 360 度.

I have an SVG that I've created in react native, and I'd simply like to rotate this 360 degrees continuously in the most efficient way possible.

谢谢.

推荐答案

只需将 SVG 包装在 View 组件中并使用动画 API.您的代码将是这样的:

Just wrap your SVG in a View component and make use of the Animated API. Your code would be something in the lines of this:

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();    
  }
}

这篇关于在 React Native 中旋转 SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆