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

相关推荐
光影少年9 小时前
react navite 页面跳转、传参、路由监听、导航栏自定义
前端·react native·react.js
光影少年2 天前
RN导航与路由
前端·react native·react.js
沉迷学习 日益消瘦3 天前
7 React Native / Expo 开发体验:项目结构、跨平台适配与构建发布
javascript·react native·react.js
太子釢5 天前
React Native Fabric 渲染链路
react native
光影少年5 天前
RN 原生动画 Animated 用法、动画性能优化
react native·react.js·掘金·金石计划
任磊abc6 天前
react native项目配置eslint+prettier
react native·eslint·prettier
sTone873756 天前
类RN框架通过Service暴露卡片渲染能力给AI Chat
android·react native
想你依然心痛7 天前
【共创季稿事节】HarmonyOS 7.0 应用框架(ArkUI-X)跨平台能力深度探索
flutter·react native·arkui-x·跨平台渲染·harmonyos 7.0·arkrender·组件兼容性
大龄秃头程序员7 天前
RN 新架构 Bridgeless 模式下原生向 RN 发事件:一次踩坑与复盘
react native