【问题标题】:Animated.spring in functional components doesn't work with onPress?功能组件中的 Animated.spring 不适用于 onPress?
【发布时间】:2020-04-17 20:51:12
【问题描述】:

我将创建一个通过单击按钮运行的动画,我通过 Animated.timing 顺利完成此操作,没有任何问题,但是当我尝试通过 Animated.spring 运行动画时,它只会运行一次又一次点击不起作用 我怎样才能解决这个问题? Animated.spring 在功能组件中运行流畅吗? 这是我的代码

const BoxShape = () => {
  const jumpValue = new Animated.Value(0);
  const ActiveAnim = () => {
    Animated.spring(jumpValue, {
      toValue: 1,
      friction: 1,
    }).start();
  }

  return (
    <View>
      <Animated.Image
        style={{ transform: [{ scale: jumpValue }], width: 120, height: 120, marginBottom: 30 }}
        source={require('.././images/ball.png')}
      >
      </Animated.Image>
      <Button
        title='Active Ball'
        onPress={ActiveAnim}
      />
    </View>
  )
}

【问题讨论】:

  • 如果你想复活,你必须重置价值。

标签: react-native animation react-animated react-functional-component


【解决方案1】:

这是正常行为,因为您在第一次运行时将值更新为 1,但从未将其重置为 0,以便动画再次运行。

我建议您在每次运行后将值重置为 0,方法是在您的代码中添加如下 sn-p :p :

const ActiveAnim = () => {
    Animated.spring(jumpValue, {
      toValue: 1,
      friction: 1,
    }).start(() => jumpValue.setValue(0));
  };

start() 接受一个函数作为参数,在动画结束后调用, https://facebook.github.io/react-native/docs/animated#configuring-animations

【讨论】:

  • 使用状态?你什么意思
  • 很抱歉,这只是一个误会,我确实测试了你的解决方案并且它有效,你的评论对我真的很有帮助,我真的很感激
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多