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

相关推荐
十步杀一人_千里不留行30 分钟前
React Native 下拉选择组件首次点击失效问题的深入分析与解决
javascript·react native·react.js
m0_7482326436 分钟前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
react native·架构·harmonyos
南望无一3 天前
React Native 0.70.x如何从本地安卓源码(ReactAndroid)构建
前端·react native
开发者每周简报4 天前
React:UI开发的革新者
javascript·react native·react.js
朦胧之12 天前
React Native 新架构 (一)
前端·react native
野槐13 天前
react实例与总结(一)
javascript·react native·react.js
GISer_Jing15 天前
React Native:跨平台移动应用开发的明星框架
javascript·react native·react.js
No Silver Bullet16 天前
ReactNative进阶(五十九):存量 react-native 项目适配 HarmonyOS NEXT
react native·react.js·harmonyos
江湖行骗老中医16 天前
React Native 开发 安卓项目构建工具Gradle的配置和使用
android·react native·react.js
@大迁世界16 天前
React Native 列表组件:FlashList、FlatList 及更多
javascript·react native·react.js·ecmascript