addListener

动画是异步的,但是某些时候你需要知道动画进行到哪一步了。比如,你有一个进度从0到1的进度动画,当这个动画进行到0.5的时候你想执行一个其他的动作。addListener方法让这成为可能。

来一些简单的代码:

this._animatedValue = new Animated.Value(0);

this._animatedValue.addListener(({value}) => this._value = value);

Animated.timing(this._animatedValue, {
    toValue: 100,
    duration: 500
}).start();

同步取值?

很多人想知道是不是能够同步的取到值。这个是不可以的。因为动画是运行在Native(本地)环境的,根本无法同步取到值。

removeListener

如果添加了Listener,那么一定要记得用完之后删掉!

results matching ""

    No results matching ""