问题描述
我有以下代码可以在 React Native 中制作动画
I have the following code to animate in React Native
Animated.timing(
this.state.absoluteChangeX,
{toValue: 0},
).start(function() {
this.lastX = 0;
this.lastY = 0;
});
很简单,但每当触发时,我都会收到错误消息:singleValue.stopTracking 不是函数
Pretty simple, but whenever it's triggered, I receive the error:singleValue.stopTracking is not a function
这里是错误的来源:
/react-native/Libraries/Animates/src/AnimtaedImplementation.js
/react-native/Libraries/Animates/src/AnimtaedImplementation.js
var timing = function(
value: AnimatedValue | AnimatedValueXY,
config: TimingAnimationConfig,
): CompositeAnimation {
return maybeVectorAnim(value, config, timing) || {
start: function(callback?: ?EndCallback): void {
var singleValue: any = value;
var singleConfig: any = config;
singleValue.stopTracking(); // <--------------- HERE!!!
if (config.toValue instanceof Animated) {
singleValue.track(new AnimatedTracking(
singleValue,
config.toValue,
TimingAnimation,
singleConfig,
callback
));
} else {
singleValue.animate(new TimingAnimation(singleConfig), callback);
}
},
stop: function(): void {
value.stopAnimation();
},
};
};
我不是很精通 typeScript,但是 var singleValue: any
意味着singleValue"可以是任何类型.就我而言,它是一个数字.由于数字没有方法,所以这会出错是有道理的.
I'm not extremely versed in typeScript, but var singleValue: any
means that "singleValue" could be any type. In my case, it's a number. Since numbers don't have methods, it would make sense that this would error.
我做错了什么吗?
推荐答案
您希望设置动画的值必须是 Animated.Value
的实例,或其子类型之一.当你初始化你的状态时,它应该看起来像这样:
The value you wish to animate must be an instance of Animated.Value
, or one of its subtypes. When you initialize your state, it should look something like this:
getInitialState() {
return { absoluteChangeX: new Animated.Value(0) };
}
框架方法中类型声明为any
的事实只是缺乏约束,而不是明确邀请将任何值传递给它.
The fact that the type declaration in the framework method is any
is just a lack of constraint, not an explicit invitation to pass any value into it.
有关更多示例,请参阅动画文档.
See the Animated docs for more examples.
这篇关于React Native Animated singleValue.stopTracking 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!