当前位置:首页 > 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
分享给朋友:

相关文章

如何降低react版本

如何降低react版本

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

vue 动画 实现

vue 动画 实现

Vue 动画实现基础 Vue 提供了 <transition> 和 <transition-group> 组件,结合 CSS 或 JavaScript 钩子实现动画效果。核心是…

如何选购react

如何选购react

选购 React 相关产品或服务的建议 React 是一个用于构建用户界面的 JavaScript 库,广泛应用于前端开发。以下是选购 React 相关产品(如课程、书籍、工具、组件库等)的参考方法:…

react 如何运行

react 如何运行

运行 React 项目的步骤 安装 Node.js 确保系统已安装 Node.js(建议版本 16+),可通过官网下载并安装。安装后验证版本: node -v npm -v 创建 React 项目…

react如何迭代

react如何迭代

在 React 中迭代数组或对象 React 提供了多种方式来处理数组或对象的迭代,以下是常见的方法: 使用 map 方法迭代数组 map 是 JavaScript 数组的内置方法,常用于在 Rea…

如何用react

如何用react

使用React的基本步骤 安装Node.js和npm 确保系统已安装Node.js和npm。Node.js自带npm,可从官网下载安装包。安装完成后,通过命令行验证版本: node -v npm…