如何使用Animated.diffClamp反应原生

Lui*_*cia 2 animation android react-native

有关如何实现Animated.diffClamp的任何代码示例?

我正在尝试创建一个标题滚动动画,就像谷歌播放应用程序中的那个.当你开始向下滚动时我已经隐藏了标题,但问题是我想在你开始向上滚动时再次显示标题,它只在你到达视图顶部时显示.

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>
  );
 }
}
Run Code Online (Sandbox Code Playgroud)

Seb*_*ber 23

下面是一个示例,标题仅在用户滚动到最小 Y 位置后从顶部出现,并通过夹紧解决过度滚动/弹跳问题。

const minScroll = 100;

const clampedScrollY = scrollY.interpolate({
  inputRange: [minScroll, minScroll + 1],
  outputRange: [0, 1],
  extrapolateLeft: 'clamp',
});

const minusScrollY = Animated.multiply(clampedScrollY, -1);

const translateY = Animated.diffClamp(
  minusScrollY,
  -AnimatedHeaderHeight,
  0,
);

const opacity = translateY.interpolate({
  inputRange: [-AnimatedHeaderHeight, 0],
  outputRange: [0.4, 1],
  extrapolate: 'clamp',
});
Run Code Online (Sandbox Code Playgroud)

更多详细信息:/sf/answers/3614680751/


Jan*_*sis 9

我们为这个用例添加了这个.这是doc页面https://facebook.github.io/react-native/docs/animated.html#diffclamp

我还建议将它与translate转换一起使用(更好的perf,可以与本机驱动程序一起使用).这是使用它的示例渲染:

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>
);
Run Code Online (Sandbox Code Playgroud)

它是如何工作的我们将滚动位置传递给diffClamp,因此在我们使用interpolate使值为负值(我们希望它向上翻译)之后,它被钳制在0到60之间.

  • 抱歉延迟,我用一个例子https://github.com/janicduplessis/collapsible-navbar做了一个回购 (5认同)
  • 我正在尝试使用本机驱动程序使用Animated.event实现diffClamp.但我似乎很少控制动画值的结果.我想制作与iOS Facebook应用程序相同的标题滚动,但我遇到了几个问题.我似乎无法阻止标题隐藏在视图顶部或重叠视图.此外,如果用户在导航栏处于幻灯片动画的中间时停止滚动,则它应该设置为完全隐藏/可见的动画.当我无法从事件中读取当前滚动值时,我不知道如何实现这些中的任何一个. (2认同)
  • @tnaught 我们能够通过在列表视图上添加 `bounces: false` 来解决我们的特定问题。这可能是也可能不是您问题的解决方案,但当时是为我们服务的。 (2认同)