美文网首页
react native回到顶部

react native回到顶部

作者: 立陶宛_d5a9 | 来源:发表于2019-08-28 22:09 被阅读0次

import React, { Component } from 'react';
import {
View,
Animated,
Easing,
ScrollView,
Button
} from 'react-native';
class Sub extends Component {
shouldComponentUpdate(nextProps){
if(nextProps.top !== this.props.top){
this.ScrollView.scrollTo({ y: nextProps.top, animated: false})
}
return true
}
ScrollView
render() {
const {AnimatedData} = this.props
return (
<ScrollView onScroll={(e)=>{
const {y} = e.nativeEvent.contentOffset
AnimatedData.ScrollTop.setValue(y)
}} ref={(ref)=>{this.ScrollView = ref}} style={{height: 600}}>
<View style={{backgroundColor: "red", height: 200}}></View>
<View style={{backgroundColor: "blue", height: 200}}></View>
<View style={{backgroundColor: "red", height: 200}}></View>
<View style={{backgroundColor: "blue", height: 200}}></View>
<View style={{backgroundColor: "red", height: 200}}></View>
<View style={{backgroundColor: "blue", height: 200}}></View>
<View style={{backgroundColor: "red", height: 200}}></View>
<View style={{backgroundColor: "blue", height: 200}}></View>
</ScrollView>
);
}
}
const ABC = Animated.createAnimatedComponent(Sub)
class App extends Component {
AnimatedData = {
ScrollTop: new Animated.Value(100)
}
render() {
return (
<View style={{flex: 1}}>
<Button title={"Top"} onPress={()=>{
Animated.timing(this.AnimatedData.ScrollTop,{
toValue: 0,
duration: 200,
easing: Easing.linear
}).start()
}}/>
<ABC top={this.AnimatedData.ScrollTop} AnimatedData={this.AnimatedData}/>
</View>
);
}
}
export default (App);

相关文章

网友评论

      本文标题:react native回到顶部

      本文链接:https://www.haomeiwen.com/subject/hpevectx.html