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

相关推荐
光影少年1 天前
react navite本地存储:AsyncStorage、MMKV、文件存储
前端·react native·react.js
梦想的颜色1 天前
AI 时代小白 VibeCoding 做 APP:UniApp(含 Uni‑X)、Flutter 与 React Native+Expo全维度技术选型对比指南
flutter·react native·app·uniapp·vibecoding·unippx·app产品
AI砖家1 天前
React Native 开发规范与完整流程指南
javascript·react native·react.js
光影少年2 天前
RN 网络请求:Fetch / Axios 封装、超时、拦截器
前端·react native·react.js
墨狂之逸才3 天前
React Native一次人脸注册引发的 MMKV 数据污染排查复盘
react native
光影少年3 天前
React Native网络、存储与原生能力
前端·react native·react.js
光影少年5 天前
react navite原生事件监听、全局事件通知
前端·react native·react.js
大龄秃头程序员7 天前
CocoaPods 静态库 & 动态库实践笔记:从 Podfile 配置到 RN 新架构的一些思考
react native