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

相关推荐
Joyee6911 天前
RN 的初版架构——UI 布局与绘制
前端·react native
Joyee6911 天前
RN 的初版架构——通信机制
前端·react native
GISer_Jing1 天前
AI/CICD/Next/React Native&Taro内容
人工智能·react native·taro
小仙女喂得猪1 天前
2025 Android原生开发者角度的Flutter 笔记整理(对比ReactNative)
android·flutter·react native
千码君20162 天前
React Native::关于react的匿名函数
javascript·react native·react.js·匿名函数·usecallback·命名函数·记忆化函数
晨旭缘2 天前
从零搭建 React Native 到项目运行全记录(0.73.6 稳定版)
javascript·react native·react.js
sure2823 天前
react native中实现水印相机功能
react native
千码君20164 天前
React Native:发现默认参数children【特殊的prop】
javascript·react native·ecmascript·react·组件树
namehu4 天前
React Native 应用性能分析与优化不完全指南
android·react native·ios
全栈探索者4 天前
ReactNative开发实战——ReactNative 开发中的图标管理方案:基于 Iconfont 的自定义图标库实现
react native