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:注意上面的代码和截图不一致;但是代码是可以实现上面的效果的。

相关推荐
一个扣子15 小时前
多环境配置:开发/生产环境下 Hermes 的开启与关闭策略
react native·开发模式·多环境配置·生产模式·hermes·环境切换·构建配置
TechMasterPlus2 天前
Hermes 深度解析:React Native 高性能 JavaScript 引擎实践指南
javascript·react native·react.js
令人头秃的代码0_02 天前
React Native Bundle更新升级
react native
用户600071819103 天前
【翻译】React Native JSI 深度解析(第 3 篇):面向 JavaScript 开发者的 C++
react native
Goway_Hui6 天前
【ReactNative鸿蒙化-三方库使用与C-API集成】
c语言·react native·harmonyos
zh_xuan9 天前
启动RN服务端口被占用
android·react native
墨狂之逸才10 天前
在 React Native 中集成 MinIO 对象存储(图片/文件上传服务)
react native
chenbin___10 天前
鸿蒙RN position: ‘absolute‘ 和 zIndex 的兼容性问题(转自千问)
前端·javascript·react native·harmonyos
未名编程10 天前
React Native WebView 加载远程页面显示错误内容的深层原因及解决方案
javascript·react native·react.js
chenbin___11 天前
检查hooks依赖的工具(转自千问)
开发语言·前端·javascript·react native·react.js