【问题标题】:Get current value of Animated.Value, React-native获取 Animated.Value 的当前值,React-native
【发布时间】:2017-06-15 09:36:07
【问题描述】:

我正在尝试使用插值对视图进行动画处理。我想获得我的 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
}

【问题讨论】:

    标签: reactjs react-native react-native-android


    【解决方案1】:

    我知道了,如何获取值:

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

    编辑

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

    console.log(this.state.translateAnim._value)
    

    【讨论】:

    • 用 tsc 给我一个警告:The property '_value' does not exist in the 'Value' type.
    • @jose920405 你有没有想过如何让它用 tsc 编译?
    • 此观察者记录在:facebook.github.io/react-native/docs/0.5/animatedvalue - 他们说“这很有用,因为无法同步读取该值,因为它可能是本机驱动的。”
    • 可以使用 //@ts-ignore 注释来忽略一行的 tsc 警告
    • 在 React 本机版本 0.59.1 中它说,addListener 不是函数。如果我错了,请纠正我。我无法以任何方式安慰动画值。
    【解决方案2】:

    这也适用于我...

    const headerHeight = new Animated.Value(0);
    

    经过一些操作......

    console.log(headerHeight.__getValue())
    

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

    【讨论】:

    • 警告:如果 userNativeDriver 为真,这将始终返回 0。
    • 在功能组件中添加 addListener 吗?
    • @roz333 我在stackoverflow.com/a/61260706/1294382下面的回答中写了一个钩子来处理这个问题
    • useNativeDriver 而不是userNativeDriver doc
    【解决方案3】:

    对于有打字稿的人。

    console.log((this.state.translateAnim as any)._value);
    

    它对我来说可以像任何人一样完整地完成 tsc。

    【讨论】:

    • 谢谢你,我不知道为什么TS显示这个错误
    【解决方案4】:

    编辑:注意 - 可能会导致严重的性能问题。我无法弄清楚为什么,但如果你将它用于 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
    }
    
    

    【讨论】:

      【解决方案5】:
      Number.parseInt(JSON.stringify(translateAnim))
      

      它适用于 React Hook

      【讨论】:

      • 非常感谢!当我尝试将 Animated.AnimatedMultiplication 的类型转换为 number 时,这对我有用
      • parseInt(JSON.stringify(translateAnim)) 也可以,没有Number infront
      【解决方案6】:

      这似乎是私有财产。但对我有用。有助于调试,但不建议在生产中使用它。

      translateAnim._value
      

      【讨论】:

      • 为什么不建议在 prod 中使用它?
      • 如果推荐用于生产,他们会公开它。
      • 是的,有道理!我最终将值存储在 Ref 中以避免任何意外行为。感谢您的警告:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      相关资源
      最近更新 更多