react-native - 为动画 React Native 重置动画
问题描述
我在 React Native 中尝试使用 resetAnimation() 时偶然发现了一个问题。
我正在尝试构建一个animated.view,当一个新视图显示在一个没有反馈的可触摸组件上时它会重置。
我无法弄清楚如何重置动画,以便当我按下我的可触摸而没有反馈时,动画会重置并重新开始显示新的动画。它在第一次渲染时运行,但随后停止并正常显示文本。
这是我的一些代码片段。
import React, { useState, useEffect } from 'react';
import { Animated, StyleSheet } from 'react-native';
const FadeView = (props) => {
const [fadeAnim] = useState(new Animated.Value(0)); // Initial value for opacity: 0
React.useEffect(() => {
Animated.timing(
fadeAnim,
{
toValue: 1,
duration: 1000,
}
).start(fadeAnim.resetAnimation())
}, []);
return (
<Animated.View // Special animatable View
style={{
...props.style,
opacity: fadeAnim, // Bind opacity to animated value
}}
>
{props.children}
</Animated.View>
);
}
const styles = StyleSheet.create({
view:{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
// You can then use your `FadeInView` in place of a `View` in your components:
export default FadeView;
以及我尝试使用它的地方。
<FadeView>
<Text style = {styles.gameText}> {question} </Text>
</FadeView>
解决方案
我设法通过删除
, []
在......的最后).start(fadeAnim.resetAnimation())
}, []);
并添加
fadeAnim.resetAnimation();
在那个代码块之后。
推荐阅读
- javascript - Threejs - 渲染不同的模型并在视野之外制作动画
- java - 我无法向我创建的 Hadoop MiniCluster 写入(放置)
- python - 在mac上使用crontab运行python脚本时出错
- javascript - 我希望这个大红色 div 像一条线一样从其原始和固定位置跟随我的自定义光标
- javascript - Boostrap 5 手风琴没有崩溃
- angular - 将库导入另一个库时“同步和异步加载角结构”
- node.js - 找不到模块“sanitize.css/page.css”
- php - 如何从 excel 文件中加载数据然后在 Laravel 中提交?
- javascript - 从网站接收数据然后触发 lambda 发送电子邮件和备份到 DynamoDB
- python - 运行命令时将数据添加到 json 文件 discord.py