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

相关推荐
gxhlh3 天前
React Native防止重复点击
javascript·react native·react.js
miao_zz3 天前
基于react native的锚点
android·react native·react.js
卿卿qing5 天前
【React Native】路由和导航
javascript·react native·react.js
日渐消瘦 - 来自一个高龄程序员的心声5 天前
react native(expo)多语言适配
javascript·react native·react.js·expo
卿卿qing5 天前
【App】React Native
javascript·react native·react.js
日渐消瘦 - 来自一个高龄程序员的心声5 天前
react native(expo)选择图片/视频并上传阿里云oss
react native·react.js·阿里云
恋猫de小郭6 天前
鸿蒙版 React Native 正式开源,ohos_react_native 了解一下
react native·react.js·harmonyos
恋猫de小郭7 天前
React Native 0.76,New Architecture 将成为默认模式,全新的 RN 来了
javascript·react native·react.js
Atypiape214 天前
(零) React Native 项目开发拾遗
android·flutter·react native·ios·typescript·rn
小童不学前端15 天前
如何在移动端app里嵌套web页面之react-native-webview
react native·expo