获取Animated.Value,React-native的当前值

Evg*_*sov 42 reactjs react-native react-native-android

我正在尝试使用插值动画查看.我想得到我的Animated.Value的当前值,但不知道如何.我不明白如何使用React-native docs.

this.state = {
      translateAnim: new Animated.Value(0)
}
DeviceEventEmitter.addListener('Accelerometer', function (data) {
  console.log(this.state.translateAnim);
  // returns an object, but I need a value in current moment
}
Run Code Online (Sandbox Code Playgroud)

Evg*_*sov 62

我发现,如何获得一个值:

this.state.translateAnim.addListener(({value}) => this._value = value);
Run Code Online (Sandbox Code Playgroud)

编辑

要记录一个值,我会执行以下操作:

console.log(this.state.translateAnim._value)
Run Code Online (Sandbox Code Playgroud)

  • 用tsc给我一个警告:`属性'_value'在'Value'类型中不存在 (4认同)
  • 可以使用 //@ts-ignore 注释来忽略一行的 tsc 警告 (2认同)

小智 12

我没有声明添加评论,但对于有typsecript的人.

console.log((this.state.translateAnim as any)._value);
Run Code Online (Sandbox Code Playgroud)

对我来说,它对任何人来说都是完整的.


Sou*_*nic 12

Number.parseInt(JSON.stringify(translateAnim))
Run Code Online (Sandbox Code Playgroud)

它适用于 React Hook

  • `parseInt(JSON.stringify(translateAnim))` 也可以工作,前面没有 `Number` (2认同)

小智 10

这对我也有用......

const headerHeight = new Animated.Value(0);
Run Code Online (Sandbox Code Playgroud)

经过一些操纵......

console.log(headerHeight.__getValue())
Run Code Online (Sandbox Code Playgroud)

它感觉很乱,但它完成了工作......

  • 警告:如果userNativeDriver为true,则始终返回0. (25认同)

ima*_*gio 6

编辑:注意 - 可能会导致严重的性能问题。我一直无法弄清楚为什么,但是如果您将它用于 30 多个同步动画,您的帧速率将慢到爬行。似乎它必须是 Animated.Value addListener 的 react-native 中的一个错误,因为我没有发现我的代码有任何问题,它只设置了一个监听器,该监听器设置了一个应该是即时的 ref。

这是我想出的一个钩子,不需要访问私有内部值就可以做到这一点。

/**
 * Since there's no (official) way to read an Animated.Value synchronously this is the best solution I could come up with
 * to have access to an up-to-date copy of the latest value without sacrificing performance.
 * 
 * @param animatedValue the Animated.Value to track
 * @param initial Optional initial value if you know it to initialize the latest value ref before the animated value listener fires for the first time
 *
 * returns a ref with the latest value of the Animated.Value and a boolean ref indicating if a value has been received yet
 */
const useAnimatedLatestValueRef = (animatedValue: Animated.Value, initial?: number) => {
    //If we're given an initial value then we can pretend we've received a value from the listener already
    const latestValueRef = useRef(initial ?? 0)
    const initialized = useRef(typeof initial == "number")

    useEffect(() => {
        const id = animatedValue.addListener((v) => {
            //Store the latest animated value
            latestValueRef.current = v.value
            //Indicate that we've recieved a value
            initialized.current = true
        })

        //Return a deregister function to clean up
        return () => animatedValue.removeListener(id)

        //Note that the behavior here isn't 100% correct if the animatedValue changes -- the returned ref
        //may refer to the previous animatedValue's latest value until the new listener returns a value
    }, [animatedValue])


    return [latestValueRef, initialized] as const
}

Run Code Online (Sandbox Code Playgroud)