React native 当nativeDriver为true时,如何获取当前动画.value?
当nativeDriver为false时,可以使用._值获取当前动画值 但当nativeDriver变为true时,是否有可能获得当前值React native 当nativeDriver为true时,如何获取当前动画.value?,react-native,react-animated,React Native,React Animated,当nativeDriver为false时,可以使用._值获取当前动画值 但当nativeDriver变为true时,是否有可能获得当前值 我们有什么解决办法吗?像这样使用addListener: this._animatedValue = new Animated.Value(0); this._animatedValue.addListener(({value}) => this._value = value); Animated.timing(this._animatedValue
我们有什么解决办法吗?像这样使用addListener:
this._animatedValue = new Animated.Value(0);
this._animatedValue.addListener(({value}) => this._value = value);
Animated.timing(this._animatedValue, {
toValue: 100,
duration: 500
}).start();
我为这样的场景找到了一个解决方案,即本地驱动程序破坏了我们的偏移量,并在动画接近尾声时使动画跳跃 创建一个伪动画值(不会用于任何动画),并将其与原始动画值一起更新。最后,动画结束后(如衰减、弹簧等),使用伪值设置偏移和当前角度,如下所示:
const angle = useRef(new Animated.Value(0)).current; //original value used in animation; will run on native side
const psuedoAngle = useRef(new Animated.Value(0)).current; //only to track the value on JS side
//using PanResponder
...
onPanResponderMove: (evt, gestureState) => {
angle.setValue(some_value);
psuedoAngle.setValue(same_value);
},
onPanResponderRelease: (evt, {vx, vy}) => {
// calculate your velocity
...
const decay1 = Animated.decay(angle,{
velocity: velocity,
deceleration: 0.997,
useNativeDriver: true
}
);
const decay2 = Animated.decay(psuedoAngle,{
velocity: velocity,
deceleration: 0.997,
useNativeDriver: false //this will keep your pseudo value intact
}
);
//set offset and current angle in callback as below
Animated.parallel([decay1,decay2]).start(() => {
psuedoAngle.flattenOffset();
angle.setOffset(psuedoAngle._value); //correct value passed to original value
psuedoAngle.setOffset(psuedoAngle._value);
angle.setValue(0);
psuedoAngle.setValue(0);
});
}
这听起来像是一个错误。你到底想做什么?@Kraylog我想实现一个快照列表。因此需要使用animated.value的实时值计算索引。因此,动画值与scrollview绑定。必须根据屏幕中心的项目显示标题。