javascript - Animated.Image 上的 React Native 交错动画接收 TypeError: undefined is not an object (evalating 'value.getValue')
问题描述
我试图错开两个动画,其中我有一个淡入屏幕的图像,然后向下平移。(我正在为种植种子制作动画)
种子是它自己的组件:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Text, View, Image, Animated, StyleSheet } from 'react-native';
import seed from '../images/seed.png';
export default class SeedComponent extends Component {
state = {
opacity: new Animated.Value(0),
movement: new Animated.Value(0)
}
componentDidMount() {
const fadeAnimation =
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 1000,
useNativeDriver: true
});
const moveAnimation =
Animated.timing(this.state.movement, {
toValue: 100,
duration: 500,
useNativeDriver: true
});
Animated.stagger(500,[
fadeAnimation,
moveAnimation
]).start();
}
render() {
const animationStyles = {
opacity: this.state.fadeAnimation,
transform: [
{ translateY: this.state.moveAnimation},
]
};
return (
<View>
<Animated.Image source={seed} style={[styles.image, animationStyles]} ></Animated.Image>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems:'center'
},
image: {
width:100,
height:100
}
});
我在 App.js 文件中导入。
但是,我不断收到以下错误:
TypeError: undefined is not an object (evaluating 'value.getValue')
我已经在
state = {
opacity: new Animated.Value(0),
movement: new Animated.Value(0)
}
所以我认为使用 ComponentDidMount 就可以了。但是,我不一定需要使用它。我只想能够在单个图像上按顺序或惊人地制作多个动画。
我怀疑错误可能在于<Animated.Image>
标签,但我无法找到解决方法。
解决方案
您是否尝试过将状态初始化移动到构造函数中?但是,我不会怀疑这是问题所在,这就是我一直这样做的方式,并且在我阅读过的所有文档中都是如此。
推荐阅读
- python - 为彩色图像而不是灰度图像训练我的简单模型
- amazon-web-services - 是否可以在 AWS 中创建 SQS 作为 LAMBDA 的事件源?
- python - openCv-python 无法读取 vs 代码中的图像
- python - 单击另一个按钮时如何删除以前的函数输出
- reactjs - 使用 React defaultProps 正确设置 prop 的初始状态
- python - 从 CharField 更改为 IntegerField - Postgres 列数据类型未更改 Django
- c# - xamarin 'Forms.Context' 已过时:'从 2.5 版开始,上下文已过时。请改用本地上下文。
- kubernetes - 遇到错误,我不理解 k8s
- java - 如何使用 selenium 模拟人类滚动弹出对话框?
- cuda - 安装cuda后显卡驱动失败