当前位置:首页 > React

react native实现动画

2026-01-26 22:58:18React

使用 Animated API 实现基础动画

React Native 提供了 Animated API 用于创建流畅的动画效果。通过定义动画值(Animated.Value)和控制动画方法(如 timingspring)实现。

import React, { useRef, useEffect } from 'react';
import { Animated, View, StyleSheet } from 'react-native';

const FadeInView = () => {
  const fadeAnim = useRef(new Animated.Value(0)).current;

  useEffect(() => {
    Animated.timing(fadeAnim, {
      toValue: 1,
      duration: 2000,
      useNativeDriver: true,
    }).start();
  }, [fadeAnim]);

  return (
    <Animated.View style={{...styles.box, opacity: fadeAnim}} />
  );
};

const styles = StyleSheet.create({
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'blue',
  }
});

组合动画效果

通过 Animated.parallelAnimated.sequence 组合多个动画,实现复杂效果。

react native实现动画

const combinedAnim = () => {
  const scaleAnim = useRef(new Animated.Value(1)).current;
  const rotateAnim = useRef(new Animated.Value(0)).current;

  useEffect(() => {
    Animated.parallel([
      Animated.spring(scaleAnim, {
        toValue: 1.5,
        friction: 2,
        useNativeDriver: true,
      }),
      Animated.timing(rotateAnim, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
      })
    ]).start();
  }, []);

  const rotateInterpolate = rotateAnim.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '360deg']
  });

  return (
    <Animated.View style={{
      transform: [
        { scale: scaleAnim },
        { rotate: rotateInterpolate }
      ]
    }} />
  );
};

使用 LayoutAnimation 实现布局动画

LayoutAnimation 适用于组件布局变化时的自动过渡动画,如列表项增删。

react native实现动画

import { LayoutAnimation, UIManager, Platform } from 'react-native';

if (Platform.OS === 'android') {
  UIManager.setLayoutAnimationEnabledExperimental(true);
}

const toggleAnimation = () => {
  LayoutAnimation.configureNext(
    LayoutAnimation.Presets.spring
  );
  // 触发状态变化导致布局更新
};

手势驱动的动画

通过 PanResponder 结合 Animated 实现拖拽等交互式动画。

const panResponder = PanResponder.create({
  onMoveShouldSetPanResponder: () => true,
  onPanResponderMove: Animated.event(
    [null, { dx: pan.x, dy: pan.y }],
    { useNativeDriver: false }
  ),
  onPanResponderRelease: () => {
    Animated.spring(pan, {
      toValue: { x: 0, y: 0 },
      useNativeDriver: true
    }).start();
  }
});

return (
  <Animated.View 
    {...panResponder.panHandlers}
    style={{ transform: pan.getTranslateTransform() }}
  />
);

使用 Reanimated 2 库实现高性能动画

当需要复杂手势或60FPS动画时,推荐使用 react-native-reanimated 库。

import Animated, {
  useSharedValue,
  withSpring,
  useAnimatedStyle,
} from 'react-native-reanimated';

const ReanimatedExample = () => {
  const offset = useSharedValue(0);

  const animatedStyles = useAnimatedStyle(() => ({
    transform: [{ translateX: offset.value * 255 }],
  }));

  return (
    <>
      <Animated.View style={[styles.box, animatedStyles]} />
      <Button 
        title="Move" 
        onPress={() => offset.value = withSpring(Math.random())}
      />
    </>
  );
};

注意事项

  • 始终添加 useNativeDriver: true 提升性能(部分动画不支持)
  • 避免在 useNativeDriver 启用时使用非变换样式(如 width/height)
  • 复杂动画建议使用 react-native-reanimated 替代原生 Animated
  • 使用 InteractionManager 处理动画与复杂任务的调度

标签: 动画react
分享给朋友:

相关文章

vue实现共享动画

vue实现共享动画

Vue 共享动画实现方法 使用 Vue Transition 组件 Vue 内置的 Transition 组件可以实现元素进入/离开的过渡效果。通过命名过渡和 mode 属性可以控制多个元素的过渡顺序…

如何降低react版本

如何降低react版本

降低 React 项目版本的步骤 检查当前 React 版本 运行以下命令查看项目中安装的 React 当前版本: npm list react 或 yarn list react 修改…

react如何遍历

react如何遍历

遍历数组或对象的方法 在React中,遍历数组或对象通常用于渲染列表或动态生成内容。以下是几种常见的遍历方法: 使用map遍历数组map是遍历数组并返回新数组的高阶函数,适合渲染列表。 con…

如何启动react

如何启动react

安装Node.js 确保系统已安装Node.js(建议使用LTS版本),可通过官网下载并安装。安装完成后,在终端运行以下命令验证版本: node -v npm -v 创建React项目 使用官方工具…

react如何安装

react如何安装

安装React的步骤 确保已安装Node.js(建议版本12或更高),可通过以下命令检查版本: node -v npm -v 使用Create React App快速搭建项目(推荐): npx c…

react如何调度

react如何调度

React 调度机制概述 React 的调度机制通过 Fiber 架构 和 Scheduler 模块实现任务优先级管理与时间切片(Time Slicing),确保高优先级更新(如用户交互)能快速响应,…