2017-04-20 37 views
2

我是否正确理解它? 这两组代码是否意味着相同的东西?它在性能或可靠性方面有什么不同吗?Animated.Event如何在React Native中工作?

<ScrollView 
onScroll={Animated.event(
    [{nativeEvent: {contentOffset: {y: this.state.scrollY}}}] 
)} 
> 
</ScrollView> 

handleScroll(e){ 
    this.setState({ scrollY : e.nativeEvent.contentOffset.y }); 
} 

<ScrollView 
onScroll={(e) => this.handleScroll(e)} 
> 
</ScrollView> 

感谢

回答

0

它是不一样的。 Animated.event用于将手势(如滚动,平移或其他事件)直接映射到动画值。所以在你的第一个例子中this.state.scrollYAnimated.Value。你可能会拥有代码的地方,初始化它,也许你的构造函数看起来是这样的:

constructor(props) { 
    super(props); 
    this.state = { 
    scrollY: new Animated.Value(0) 
    }; 
} 

在你的第二个例子this.state.scrollY的是,在滚动事件触发的y值(只数),但完全与动画无关。所以你不能使用该值,因为你可以在动画中使用Animated.Value

it's explained here in the documentation

相关问题