2017-04-20 93 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

相關問題