简体   繁体   English

如何在 React Native 中为 svg 文件的一部分设置动画

[英]How to animate a part of an svg file in React Native

I'm working on a react native app, and I need to animate (rotate around central point) a part of an svg file.我正在开发一个反应本机应用程序,我需要对 svg 文件的一部分进行动画处理(围绕中心点旋转)。 how can I access the component I want to animate?如何访问我想要制作动画的组件?

I've tried to convert the svg file in jsx format.我尝试将 svg 文件转换为 jsx 格式。 but still I cannot access the component I want to rotate但我仍然无法访问要旋转的组件

App.js:应用程序.js:

import React from 'react';
import { StyleSheet, Text, View, Animated } from 'react-native';
import SvgComponent from './assets/hotFan';


class App 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}]}}>
          <SvgComponent/>
      </Animated.View>
    );
  }

  componentDidMount() {

    Animated.loop(
      Animated.timing(this.animation, {toValue: 1, duration: 2000})
    ).start();    
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;

I cannot put the whole component code here as it surpasses the characters limit.我不能把整个组件代码放在这里,因为它超过了字符限制。 but you can convert https://drive.google.com/file/d/1lKaSWTO1_0cQQN6rtdaw_yWE9qa5nSjV/view?usp=sharing to jsx file using https://www.smooth-code.com/open-source/svgr/playground/但是您可以使用https ://www.smooth-source// 将https://drive.google.com/file/d/1lKaSWTO1_0cQQN6rtdaw_yWE9qa5nSjV/view?usp=sharing转换为 jsx 文件。

the actual code rotates the whole component around, but the internal arrow is the one that is supposed to rotate instead of the whole component.实际代码旋转整个组件,但内部箭头是应该旋转的,而不是整个组件。

You can split the SVG into two components: arrow component, which is the static part of the SVG and a Fan component - the animated part.您可以将 SVG 拆分为两个组件:箭头组件,它是 SVG 的 static 部分和一个Fan组件 - 动画部分。 Then just wrap the Fan component with Animated.View and pass you animation:然后只需用Animated.View包装Fan组件并传递给您 animation:

 <View>
     <SVG />
     <Animated.View style={animatedStyle}>
        <Fan />
     </Animated.View>
 </View>

The animated component will be positioned "absolute" in the wrapped and will render the animation properties, too:动画组件将在包装中“绝对”定位,并将呈现 animation 属性:

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

const animatedStyle = {
    position: 'absolute',
    top: 0,
    left: 0,
    transform: [
        { rotate: interpolateRotation }
    ]
}

Finally, the easiest part is the prepare the animation and to start it:最后,最简单的部分是准备 animation 并启动它:

animatedValue = new Animated.Value(1);

componentDidMount() {
    this.startAnimation();
}

startAnimation = () => {
    this.animatedValue.setValue(0);
    Animated.timing(this.animatedValue, {
      toValue: 1,
      duration: 1500,
      easing: Easing.linear,
    }).start(() => this.startAnimation())
}

I have created a working demo here .我在这里创建了一个工作演示。

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

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