React native引入新的Animated API,我想制作一个循环动画,比如一个气泡放大,然后缩小,然后重复这个过程.

但是我想不出来.我试着写一些如下的代码

class TestProject extends React.Component {

  constructor(): void {
    super();
    this.state = {
      bounceValue: new Animated.Value(0),
      v: 1,
    };
  }

  componentDidMount() {
    this.state.bounceValue.setValue(1.5);

    let animation = Animated.timing(this.state.bounceValue, {
      toValue: this.state.v,
    });

    setInterval(() => {
      animation.stop();

      if (this.state.flag) {
        this.state.v = 0.5;
        this.state.bounceValue.setValue(0.5);
      }
      else {
        this.state.v = 1.5;
        this.state.bounceValue.setValue(1.5);
      }

      animation.start();
    }, 5000);

  }

  render(): ReactElement {
    return (
      <View style={styles.imageContainer}>
        <Image
          style={styles.image}
          source={{uri: 'http://image142-c.poco.cn/best_pocoers/20130517/91062013051716553599334223.jpg'}}
        />
        <Animated.Text
          style={[
            styles.test,
            {transform: [
              {scale: this.state.bounceValue},
            ],}
          ]
          }>
          haha
        </Animated.Text>
      </View>
    );
  }

}

但效果不是很好.

任何建议都将不胜感激.

推荐答案

现在有loop animation个:

Animated.loop(
  Animated.sequence([
    Animated.timing(this.state.animatedStartValue, {
      toValue: 1,
      duration: 500,
      delay: 1000
    }),
    Animated.timing(this.state.animatedStartValue, {
      toValue: 0,
      duration: 500
    })
  ]),
  {
    iterations: 4
  }
).start()

React-native相关问答推荐

没有给出相同价值观的时刻

出现错误:使用 process(css).then(cb) 来使用异步插件

无法解析 com.google.gms:google-services:4.3.15

元素类型无效:应为字符串

如何在 Recompose 中使用 withHandlers 将 refs 添加到功能组件并在 ScrollView 上调用 ScrollTo?

错误发生意外错误:https://registry.yarnpkg.com/react-native-template-react-native-template-typescript:未找到

react-native react-native-vector-icons:如何使用字体真棒图标

React Native 响应式图像宽度,而图像的宽度/高度比保持不变

如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL

React Native 错误:Animated.event now requires a second argument for options

react-native 从右到左

在本react-native中具有异步和等待的箭头函数

React Native:如何获取文件大小、mime 类型和扩展名?

react-native fetch() cookie 持久化

如何在 Text 中围绕 Text 包裹 TouchableOpacity?

React Native - React Navigation导航转换

Xcode 12 - 没有要编译的架构(ONLY_ACTIVE_ARCH=YES,active arch=x86_64,VALID_ARCHS=arm64e armv7s arm64 arm7)

React Native - 如何从推送通知中打开路由

使用expo react-native链接?

React Native - 导航问题undefined is not an object (this.props.navigation.navigate)