【问题标题】:On scroll React Native Animated do the animation but flickering在滚动 React Native Animated 上做动画但闪烁
【发布时间】:2019-03-27 08:54:27
【问题描述】:

在我的反应原生应用程序中,我试图为视图的不透明度设置动画。 当我滚动时,我看到动画完成了这项工作,但它同时闪烁。我不知道为什么。

视频示例:https://cdn.discordapp.com/attachments/102861040538120192/560165613092339734/video.mov

这是我编写的代码

const Scrollable = () => {
  const largeHeaderSize = useState({
    height: 0,
    y: 0
  });

  const animatedValueScrollY = new Animated.Value(largeHeaderSize.height);
  const [scrollY, setScrollY] = useState(0);

  const headerOpacity = animatedValueScrollY.interpolate({
    inputRange: [0, largeHeaderSize.height],
    outputRange: [1, 0],
    extrapolate: "clamp"
  });

  return (
    <SafeAreaView>
      <Animated.View
        style={{
        borderBottomWidth:
          scrollY >= largeHeaderSize.height ? StyleSheet.hairlineWidth : 0
      }}>
        <View>
          <Animated.View style={{ zIndex: 1, opacity: headerOpacity }}>
            <Text>Title</Text>
          </Animated.View>
        </View>
      </Animated.View>
      <Animated.ScrollView
        onScroll={Animated.event(
          [{ nativeEvent: { contentOffset: { y: animatedValueScrollY } } }],
          {
            useNativeDriver: true,
            listener: event => setScrollY(event.nativeEvent.contentOffset.y)
          }
        )}
        scrollEventThrottle={16}
        contentInset={{
          top: 0,
          bottom: 50
        }}
        contentOffset={{
          y: 0
        }}
      />
    </SafeAreaView>
  );
};

我该如何解决?

【问题讨论】:

  • 在开发者菜单测试动画时尝试禁用 JS 开发模式 ...
  • @HendEl-Sahli 我在真实设备上也有问题?你认为它可以解决这个问题吗?
  • 模拟器和物理设备都是一样的……在测试动画时总是建议禁用JS开发模式
  • 这并没有解决问题:(

标签: javascript ios react-native animation react-hooks


【解决方案1】:

解决方案是像这样使用 useRef 钩子:

const animatedValueScrollY = useRef(new Animated.Value(0))

const headerOpacity = animatedValueScrollY.current.interpolate({
   inputRange: [0, largeHeaderSize.height],
   outputRange: [1, 0],
   extrapolate: 'clamp'
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2018-02-08
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多