javascript - React Native 中的循环运动动画

标签 javascript react-native animation

我需要创建一个动画,其中一个图像将环绕另一个图像。我已经尝试使用类似问题的建议,如 Animate a Circle around another circle ,但不幸的是它没有帮助。我尝试研究可提供理想功能的第 3 方模块,但没有找到满足我需要的东西。

我找到了一个有用的 article理解 JavaScript 中的圆周运动,但是我很难在 React Native 动画中复制它。我相信我很难理解 Animated API 的正确用法和 transform 动画圆周运动时的样式属性。


<View style={animationContainer}>
    <Image
      source={require('./images/image.png')}
      style={image}
    />
    <Animated.Image
      source={require('./images/icon.png')}
      style={circlingIcon}
    />
</View>

最佳答案

我已经为问题 react native circle transform translate animation 发布了类似的butter smooth 解决方案

完整代码:


import React, {Component} from 'react';
import {View, Text, Animated, StyleSheet, Easing} from 'react-native';

export default class Circle extends Component {
    constructor() {
        super();
        this.animated = new Animated.Value(0);
        var inputRange = [0, 1];
        var outputRange = ['0deg', '360deg'];
        this.rotate = this.animated.interpolate({inputRange, outputRange});
        outputRange = ['0deg', '-360deg'];
        this.rotateOpposit = this.animated.interpolate({inputRange, outputRange});
    }

    componentDidMount() {
        this.animate();
    }

    animate() {
      Animated.loop(
        Animated.timing(this.animated, {
            toValue: 1,
            duration: 4000,
            useNativeDriver: true,
            easing: Easing.linear,
        }),
      ).start();
    }
    render() {
        const transform = [{rotate: this.rotate}];
        const transform1 = [{rotate: this.rotateOpposit}];
        return (
          <View style={styles.container}>
            <Animated.View style={[styles.item, {transform}]}>
              <Animated.View style={[styles.topItem, {transform: transform1}]}>
                <Text style={styles.text}>Test</Text>
              </Animated.View>
            </Animated.View>
          </View>
        );
    }
 }
 const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    item: {
        position: 'absolute',
        width: 100,
        height: 200, // this is the diameter of circle
    },
    topItem: {
        width: '100%',
        height: 20,
        backgroundColor: 'red',
        position: 'absolute',
        alignItems: 'center',
        justifyContent: 'center',
    },
    text: {
        color: '#fff',
    },
 });

希望对你有帮助

关于javascript - React Native 中的循环运动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60049796/

相关文章:

javascript - 使用 JS 和 jQuery 的响应式文本

javascript - 如何使用工作场所 graphql api 发布多图像

javascript - 如何使用 angularjs 将多个表单数据一次性发送到服务器?

Swift - 动画在切换 View 和返回时不起作用

javascript - 图层.删除(图像);无法在 Kinetic.js 中工作

react-native - 无法在 React-Native 中获取 fetch(url)

javascript - React-Native Flatlist 会在状态发生变化时重新渲染图像

android - 是否可以在react-native datePicker中设置minTime和maxTime?

javascript - 如何为测试禁用 vue.js 转换?

css - 将 CSS 动画与页面的其余部分隔离开来