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

相关推荐
sure28211 小时前
React Native中自定义TabBar
前端·react native·react.js
前端Hardy1 天前
Flutter vs React Native vs HarmonyOS:谁更适合下一代跨端?2026 年技术选型终极指南
前端·flutter·react native
blanks20202 天前
react native 0.84 环境搭建
react native·reactivecocoa
无巧不成书02183 天前
React Native 深度解析:从架构到实战
react native·react.js·架构
墨狂之逸才3 天前
React Native 远程多语言动态更新方案详解
react native
无巧不成书02183 天前
React Native 深度解析:跨平台移动开发框架(2026实战版)
javascript·react native·react.js
sealaugh323 天前
react native(学习笔记第一课)环境构筑(hello,world)
笔记·学习·react native
sure2825 天前
React Native中创建自定义渐变色
前端·react native
墨狂之逸才5 天前
React Native 物理按键扫码监听终极方案:从冲突到完美共存
react native
是梦终空9 天前
React Native 性能优化指南
react native·性能优化