关于如何实现 Animated.diffClamp 的任何代码示例?
我正在尝试创建一个标题滚动动画,就像 google play 应用中的特色动画一样。当您开始向下滚动时我已经隐藏了标题,但问题是我想在您开始向上滚动时再次显示标题,它仅在您到达 View 顶部时显示。
class Services extends Component {
constructor(props){
super(props);
this.state = {
scrollY : new Animated.Value(0),
}
}
renderScrollViewContent(){
return (
<View style={styles.scrollViewContent}>
</View>
)
}
render() {
const headerHeight = this.state.scrollY.interpolate({
inputRange: [0, 60],
outputRange: [0, -60],
extrapolate: 'clamp'
});
return (
<View style={styles.container}>
<ScrollView style={styles.container}
scrollEventThrottle={16}
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.scrollY}}}]
)}
>
{this.renderScrollViewContent()}
</ScrollView>
<Animated.View style={[styles.header, {top: headerHeight}]}>
<View style={styles.bar}>
<Text style={styles.title}>Title</Text>
</View>
</Animated.View>
</View>
);
}
}
最佳答案
我们正是为这个用例添加了这个。这是文档页面 https://reactnative.dev/docs/animated#diffclamp
我还建议将它与翻译转换一起使用(更好的性能并且可以与 native 驱动程序一起使用)。这是您使用它进行渲染的示例:
const headerTranslate = Animated.diffClamp(this.state.scrollY, 0, 60)
.interpolate({
inputRange: [0, 1],
outputRange: [0, -1],
});
return (
<View style={styles.container}>
<ScrollView style={styles.container}
scrollEventThrottle={16}
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {y: this.state.scrollY}}}]
)}
>
{this.renderScrollViewContent()}
</ScrollView>
<Animated.View style={[styles.header, {transform: [{translateY: headerTranslate}]}]}>
<View style={styles.bar}>
<Text style={styles.title}>Title</Text>
</View>
</Animated.View>
</View>
);
它的工作原理是我们将滚动位置传递给 diffClamp,以便在我们使用插值使值变为负值(我们希望它向上平移)后将其限制在 0 到 60 之间。
关于android - 如何在 React Native 中使用 Animated.diffClamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901966/