javascript - 我如何循环动画循环进度组件

标签 javascript reactjs typescript react-native

我正在使用react-native-circular-progress组件,并且我试图每30秒循环一次动画。

即动画时长 30 秒,我希望它在完成后立即重新启动

该组件公开了一个名为 reAnimate 的函数,我在组件安装后立即将其放入 setInterval 函数中。

import React from 'react';
import { StyleSheet, Text, View,Dimensions, Easing } from 'react-native';
import { AnimatedCircularProgress } from 'react-native-circular-progress';

const MAX_POINTS = 30;
export default class App extends React.Component {
    state = {
        isMoving: false,
        pointsDelta: 0,
        points: 1,
    };

    componentDidMount(){

        setInterval(
            () => this.circularProgress.reAnimate(0,100, 30000,Easing.linear),
            30000
        );
    }

    render() {
        const { width } = Dimensions.get("window");
        const window = width - 120;
        const fill = (this.state.points / MAX_POINTS) * 100;
        return (
            <View style={styles.container} >

            <AnimatedCircularProgress
                size={window}
                width={7}
                backgroundWidth={5}
                fill={0}
                tintColor="#ef9837"
                backgroundColor="#3d5875"
                ref={(ref) => this.circularProgress = ref}
                arcSweepAngle={240}
                rotation={240}
                lineCap="round"
            >
                {fill => <Text style={styles.points}>{Math.round((MAX_POINTS * fill) / 100)}</Text>}

            </AnimatedCircularProgress>

            </View>
        );
    }
}

const styles = StyleSheet.create({
    points: {
        textAlign: 'center',
        color: '#ef9837',
        fontSize: 50,
        fontWeight: '100',
    },
    container: {
        flex: 1,
        justifyContent: 'space-between',
        alignItems: 'center',
        backgroundColor: '#0d131d',
        padding: 50,
    },
    pointsDelta: {
        color: '#4c6479',
        fontSize: 50,
        fontWeight: '100',
    },
    pointsDeltaActive: {
        color: '#fff',
    },
});

这正在工作...但是...动画仅在组件安装后30s开始。如何让它立即循环?

任何帮助将不胜感激。

谢谢。

最佳答案

原因是 setInterval 不会立即触发,它会在您经过的 duration 之后开始,即 30 分钟,所以您所要做的就是在设置间隔之前先调用电话,也不要忘记清除间隔。

我会这样做:

componentDidMount(){
  this.circularProgress.animate(100, 30000,Easing.linear);

  this.intervalId = setInterval(
    () => this.circularProgress.reAnimate(0,100, 30000,Easing.linear),
    30000
  );
}

componentWillUnmount() {
  clearInterval(this.intervalId);
}

关于javascript - 我如何循环动画循环进度组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57114233/

相关文章:

Visual Studio 中的 Typescript AMD 模块

typescript - 为什么交集会阻止将接受任何类型的值设置为字符串?

javascript - 无法使用 next.js 加载静态资源

css - 在 css-loader 中修改 localIdentName 在 webpack 5 react 17 中不起作用

javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告

javascript - 为什么我的 localStorage.setItem 似乎没有做任何事情?

javascript - 使用 require.js 依赖注入(inject)

javascript - 了解 JavaScript 中的嵌套 for 循环

javascript - 单击外部以隐藏带有按钮的容器

node.js - 我在我的 nestJS 应用程序(使用 authGuard)中使用 passport-jwt 身份验证策略,如何访问我的 Controller 中的 token 有效负载?