代码之家  ›  专栏  ›  技术社区  ›  Mahdi Bashirpour

在react native中使用requestAnimationFrame

  •  2
  • Mahdi Bashirpour  · 技术社区  · 6 年前

    如何在react native中使用requestAnimationFrame。

    我用这个来实现这样的可触摸不透明度

    this.requestAnimationFrame(() => {
        if (this.state.scrollEnabled)
            this._panel.transitionTo({toValue: 0});
        else {
            this.setState({scrollEnabled: true}, () => {
                this._panel.transitionTo({toValue: height})
            })
        }
    });
    

    并返回此错误

    此.requestAnimationFrame不是函数

    2 回复  |  直到 5 年前
        1
  •  9
  •   hong4rc 6ark    6 年前

    你必须移除 this 从第一行:

    requestAnimationFrame(() => {
            ....
    });
    
        2
  •  1
  •   Noel De Martin    5 年前

    实际上,建议不要使用诸如 requestAnimationFrame 直接在react native中。建议使用 TimerMixin 相反,这样做最终会像最初问题中的代码一样调用它。

    所以,一个更好的答案可能是“你错过了时间机器蛋白”。

    以下是解释这一点的官方文件: https://facebook.github.io/react-native/docs/timers#timermixin