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

相关文章

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要包括内置组件和第三方库。 使用 <transition> 和 <transition-group> Vue 内置…

vue实现共享动画

vue实现共享动画

Vue 实现共享动画的方法 在 Vue 中实现共享动画(即多个元素间平滑过渡的动画效果)可以通过 Vue 的过渡系统和第三方库(如 GSAP)来实现。以下是几种常见方法: 使用 Vue 的 <…

webstorm如何运行react

webstorm如何运行react

运行 React 项目的方法 在 WebStorm 中运行 React 项目需要确保项目已正确配置,并且依赖项已安装。以下是具体操作步骤: 确保项目依赖已安装 打开终端(Terminal),导航到项…

react如何引用jquery

react如何引用jquery

在React项目中引用jQuery可以通过以下方法实现: 安装jQuery依赖 通过npm或yarn安装jQuery包: npm install jquery # 或 yarn add jquer…

react如何引用传递

react如何引用传递

引用传递的概念 在React中,引用传递(Ref Forwarding)是一种将ref属性自动传递给子组件的技术。这对于高阶组件(HOC)或封装第三方组件时非常有用,允许父组件直接访问子组件的DOM节…

react如何部署linux

react如何部署linux

部署 React 应用到 Linux 服务器的步骤 构建生产环境代码 在本地开发环境中运行构建命令,生成优化后的静态文件: npm run build 这会生成一个 build 目录,包含所有压缩和…