ReactNative实现宽度变化实现的动画效果

效果如上图所示,通过修改设备宽度实现动画效果

复制代码
import React, {useRef, useEffect, useState} from 'react';
import {Animated, Text, View, Image} from 'react-native';

const FadeInView = props => {
  const fadeAnim = useRef(new Animated.Value(0)).current;

  React.useEffect(() => {
    Animated.timing(
      fadeAnim, // 动画中的变量值
      {
        toValue: props.currentWidth, // 
        duration: props.durationTime, // 让动画持续一段时间
        //  Style property 'width' is not supported by native animated module
        useNativeDriver: false,
      },
    ).start(); // 开始执行动画
  }, [props.currentWidth]);

  return (
    <Animated.View // 使用专门的可动画化的View组件
      style={{
        ...props.style,
        width: fadeAnim, // 将宽度绑定到动画变量值
      }}>
   
    </Animated.View>
  );
};

// 然后你就可以在组件中像使用`View`那样去使用`FadeInView`了
export default props => {
  return (
    <FadeInView
      durationTime={props.durationTime}
      currentWidth={props.currentWidth}
      style={{height: 310, backgroundColor: 'pink'}}></FadeInView>
  );
};

使用的代码如下

复制代码
<LayoutAnimatedWidth
      currentWidth={this.state.currentWidth}
      durationTime={this.state.durationTime}>
</LayoutAnimatedWidth>

PS:注意上面的代码和截图不一致;但是代码是可以实现上面的效果的。

相关推荐
努力往上爬de蜗牛1 天前
react native真机调试
javascript·react native·react.js
o***Y3631 天前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
react native·架构·harmonyos
5***a9752 天前
React Native性能优化技巧
javascript·react native·react.js
wordbaby3 天前
React Native 进阶实战:基于 Server-Driven UI 的动态表单架构设计
前端·react native·react.js
胡琦博客4 天前
21天开源鸿蒙训练营|Day2 ReactNative 开发 OpenHarmony 应用环境搭建实录
javascript·react native·react.js
6***37944 天前
React Native热更新方案
javascript·react native·react.js
x***J3484 天前
React Native组件封装
javascript·react native·react.js
E***U9454 天前
React Native开发
android·react native·react.js
t***L2664 天前
React Native真机调试连接不上的解决
javascript·react native·react.js
Tamarous4 天前
React Native 通信机制详解 - 新架构
react native