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

相关推荐
wordbaby2 天前
如何封装一个生产级的 React Native 分页列表 Hook
前端·react native·react.js
沐言人生4 天前
ReactNative 源码分析5——ReactActivity之启动RN应用
android·react native
沐言人生5 天前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
沐言人生6 天前
ReactNative 源码分析3——ReactActivity之初始化RN应用
android·react native
一个扣子6 天前
Hermes 未来路线图:2025 年起的新特性与 React Native New Architecture 协同
react native·未来发展·路线图·hermes·字节码diffing·性能增强
沐言人生6 天前
React Native 源码分析1——HybridData 机制深度分析
android·react native
空中海6 天前
01 React Native 基础、核心组件与布局体系
javascript·react native·react.js
Yue1687 天前
一文教你五分钟学会Zustand,React状态管理更加方便!
react native
空中海7 天前
03 性能、动画与 React Native 新架构
react native·react.js·架构
空中海7 天前
02 React Native状态、导航、数据流与设备能力
javascript·react native·react.js