3

需要一些JS的帮助。是否可以根据需要绑定动画事件?反应本地绑定动画事件

我需要这样做:

onScroll={ 
    Animated.event([ 
     { 
      nativeEvent: { 
       contentOffset: {y: this.state.animTop} 
      } 
     } 
    ]) 
}  

我还需要做到这一点

onScroll={(e) => { 
    let positionY = e.nativeEvent.contentOffset.y; 
    this._handleScroll(positionY); 
    this.setState({y: positionY}) 

}} 

我曾经尝试都像这样绑定,但它不采取做Animated.event

componentDidMount() { 
    this._handleScroll = this._handleScroll.bind(this); 
} 
onScroll={ 
    this._handleScroll 
} 
_handleScroll(e) { 
    Animated.event([ 
     { 
      nativeEvent: { 
       contentOffset: {y: this.state.animTop} 
      } 
     } 
    ]); 
    if(e > 30) { 
     this.setState({statusBarHidden: true}); 
    } else { 
     this.setState({statusBarHidden: false}); 
    } 
} 

回答

8

终于搞定了:

绑定功能的Animated.event听众:

onScroll={Animated.event(
        [{ nativeEvent: { contentOffset: { y: this.state.animTop } } }], 
        { listener: this._handleScroll }, 
       )} 
+0

这是为你工作在iOS和Android? – GollyJer

1

你也可以使用setValue()

因此,这将这样的工作:

_handleScroll(event) { 
    // Do stuff 
    this.state.animTop.setValue(event.nativeEvent.contentOffset.y); 
    // Do other stuff 
}