当前位置:首页 > 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 组合多个动画,实现复杂效果。

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 适用于组件布局变化时的自动过渡动画,如列表项增删。

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 库。

react native实现动画

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 native如何启动

react native如何启动

如何启动 React Native 项目 安装 Node.js 和 npm 确保已安装 Node.js(建议版本 16 或更高)和 npm(Node.js 自带)。可通过以下命令检查版本: node…

react如何发音

react如何发音

React的发音 React的正确发音为 /riˈækt/,类似于“ree-akt”。以下是详细说明: 发音分解 第一个音节“Ree”发音类似英文单词“see”中的“ee”音。…

react如何清理

react如何清理

清理 React 项目的方法 清理未使用的依赖项 运行 npm prune 或 yarn install --production 可以移除 node_modules 中未在 package.json…

如何设计react组件

如何设计react组件

设计 React 组件的核心原则 React 组件的设计需要遵循高内聚、低耦合的原则,确保组件功能独立且易于维护。组件的设计可以分为展示组件和容器组件两类,展示组件负责 UI 渲染,容器组件负责逻辑处…

如何运行react项目

如何运行react项目

运行React项目的步骤 确保已安装Node.js(建议版本12或更高)和npm/yarn。若未安装,可从Node.js官网下载。 全局安装create-react-app脚手架工具(若已安装可跳过…

react如何抛异常

react如何抛异常

如何在 React 中抛出异常 在 React 中,异常处理通常分为组件内部错误和全局错误捕获两种方式。以下是具体实现方法: 组件内部抛出异常 直接使用 JavaScript 的 throw 语句…