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

相关推荐
浩风祭月2 天前
React 18 并发特性实战:用 useTransition 和 useDeferredValue 优化列表搜索体验
前端·react native
老王以为2 天前
单仓库下的四十模块 —— React Monorepo 工程架构拆解
前端·react native·react.js
墨狂之逸才3 天前
npm/yarn 注册表(Registry)与 .npmrc 配置指南
react native
ImTryCatchException3 天前
React Native 嵌入现有 Android 项目:踩坑记录与解决方案
android·react native·react.js
花椒技术3 天前
复杂直播业务做 RN 跨端,我们最后保留了哪些 Native 边界
react native·react.js·harmonyos
wordbaby5 天前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native
wordbaby7 天前
React Native + RNOH:一个 `lazyScreen()` 搞定 48 页面启动懒加载
前端·react native
wordbaby8 天前
React Native 压缩上传全链路方案:从架构设计到生产实践
前端·react native
沙漠8 天前
React Native-SyncFormatEdittext:用 JSI 实现零闪烁的实时文本格式化
前端·react native
野槐9 天前
React Native基础
javascript·react native·react.js