由于某些原因,我看不到我的代码做错了什么。正如文档显示的那样,我似乎在使用Animated,但这个错误总是出现。代码片段:
import React ,{ Component} from "react";
import { Card } from 'react-native-elements';
import { Text, View ,Animated,Easing} from 'react-native';
import { connect } from 'react-redux';
import { baseUrl } from '../shared/baseUrl';
import { Loading } from './LoadingComponent';
const mapStateToProps = state => {
return {
dishes: state.dishes,
comments: state.comments,
promotions: state.promotions,
leaders: state.leaders
}
}
function RenderItem(props) {
const item = props.item;
if (item != null) {
return(
<Card
featuredTitle={item.name}
featuredSubtitle={item.designation}
image={{uri: baseUrl + item.image}}>
<Text
style={{margin: 10}}>
{item.description}</Text>
</Card>
);
}
}
class Home extends Component{
constructor(props) {
super(props);
this.animatedValue = new Animated.Value(0);
}
componentDidMount () {
this.animate()
}
animate () {
this.animatedValue.setValue(0)
Animated.timing(
this.animatedValue,
{
toValue: 8,
duration: 8000,
easing: Easing.linear
}
).start(() => this.animate())
}
render() {
const xpos1 = this.animatedValue.interpolate({
inputRange:[0,1,3,5,8],
outputRange:[1200 , 600 , 0 , -600 , -1200]
});
return(
<View style = {{flex :1 , flexDirection:'row' , justifyContent : 'center'}}>
<Animated.View style={{width :'100%' , tranform :[{translateX:xpos1}]}}>
<RenderItem item={this.props.dishes.dishes.filter((dish) => dish.featured)[0]}
isLoading={this.props.dishes.isLoading}
erreMess={this.props.dishes.erreMess}
/>
</Animated.View>
</View>
);
}
}
export default connect(mapStateToProps)(Home);
我正在尝试实现animation.timing函数。正如文档中所提到的,我使用在构造函数中声明的animatedValue编写了animation.timing函数。但是当我编写动画函数时,我得到了以下错误:
我遵循了react动画的文档,并在互联网上尝试了所有方法来解决这个问题,但都失败了。
发布于 2020-04-14 15:48:39
我认为您可能只想循环动画,而不是尝试手动“重置”时间,这在文档中是不推荐的。
连续循环给定的动画,以便每次到达末尾时,它都会重置并从头开始。
constructor(props) {
super(props);
this.state = {
animatedValue: new Animated.Value(0),
}
}
componentDidMount () {
this.animate();
}
animate = () => {
Animated.loop(
Animated.timing(
this.state.animatedValue,
{
useNativeDriver: true, // loop W/O blocking main JS thread
toValue: 8,
duration: 8000,
easing: Easing.linear
}
),
).start()
}
https://stackoverflow.com/questions/61202131
复制相似问题