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 天前
ReactNative从入门到性能优化(一)
前端·react native·客户端
ideaout技术团队4 天前
android集成react native组件踩坑笔记(Activity局部展示RN的组件)
android·javascript·笔记·react native·react.js
洞窝技术5 天前
前端开发APP之跨平台开发(ReactNative0.74.5)
android·react native·ios
光影少年5 天前
React Native 第三章
javascript·react native·react.js
光影少年6 天前
React Navite 第二章
前端·react native·react.js·前端框架
月弦笙音7 天前
【React】19深度解析:掌握新一代React特性
javascript·react native·react.js
Amy_cx8 天前
搭建React Native开发环境
javascript·react native·react.js
_pengliang9 天前
React Native 使用 react-native-credentials-manager 接入谷歌登录教程
javascript·react native·react.js
诚实可靠王大锤11 天前
react-native实现多列表左右滑动+滚动TabBar悬停
javascript·react native·react.js·1024程序员节