使用 Animated.createAnimatedComponent(ScrollView)
时创建动画ScrollView
无法再使用 scrollTo
.
const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);
<AnimatedScrollView ref={(ref) => this.list = ref}>
<View style={{height: 1000}} />
</AnimatedScrollView>
调用
this.list.scrollTo({x: 0, y: 0})
给出以下错误:_this.list.scrollTo is not a function
它在普通的 ScrollView 上工作正常。有没有办法解决这个问题?
最佳答案
@max23_ 的答案现在可能有效,但不是正确的方法——根据经验,我们永远不应该直接访问私有(private)变量,因为这些变量经常会发生变化。 (编辑:没有不尊重:-))
由于this pull request ,获得包装组件的 ref
的新的和面向 future 的方法是使用getNode()
实用程序方法,因为访问私有(private)变量(以 _
开头)对于 future 的 API 更改是不安全的。
所以,新的做法是
ref={c => (this.myRef = c)}
然后在调用方法时,做
this.myRef.getNode().scrollTo({
y: 0,
animated: true,
});
:-)
关于react-native - scrollTo 在动画 ScrollView 上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051368/