mcpField
R
react-native-animations

react-native-animations

github.com/pluginagentmarketplace设计与 UI10663 次安装42 周前更新
代码仓库

关于 react-native-animations

react-native-animations 是面向 React Native 开发者的动画学习技能,涵盖 Reanimated 3、Gesture Handler 和布局动画,并包含性能优化与常见错误处理。

主要功能

  1. 01使用 Reanimated 3 创建动画
  2. 02通过 Gesture Handler 处理手势
  3. 03实现布局进入和退出动画
  4. 04组合手势与动画
  5. 05排查常见动画和手势错误

适用场景

  1. 01制作弹簧或定时动画
  2. 02开发可拖动的交互元素
  3. 03为列表项添加布局动画
  4. 04排查手势未响应的问题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/pluginagentmarketplace/custom-plugin-react-native --skill react-native-animations

安装后在 Claude Code 中输入/react-native-animations确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

assets/
config.yaml
schema.json
references/
GUIDE.md
PATTERNS.md
scripts/
validate.py

详细介绍

React Native 动画技能

学习如何使用 Reanimated 3、Gesture Handler 和布局动画实现高性能动画。

前提条件

  • React Native 基础知识
  • 了解 JavaScript 闭包
  • 熟悉变换和样式

学习目标

完成此技能后,你将能够:

  • 使用 Reanimated 创建流畅的 60fps 动画
  • 使用 Gesture Handler 处理复杂手势
  • 实现布局进入和退出动画
  • 优化动画性能
  • 将手势与动画结合使用

涵盖主题

1. 安装
npm install react-native-reanimated react-native-gesture-handler

# babel.config.js
module.exports = {
  plugins: ['react-native-reanimated/plugin'],
};
2. Reanimated 基础知识
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
} from 'react-native-reanimated';

function AnimatedBox() {
  const scale = useSharedValue(1);

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [{ scale: scale.value }],
  }));

  const handlePress = () => {
    scale.value = withSpring(scale.value === 1 ? 1.5 : 1);
  };

  return (
    <Pressable onPress={handlePress}>
      <Animated.View style={[styles.box, animatedStyle]} />
    </Pressable>
  );
}
3. Gesture Handler
import { Gesture, GestureDetector } from 'react-native-gesture-handler';

function DraggableBox() {
  const translateX = useSharedValue(0);
  const translateY = useSharedValue(0);

  const pan = Gesture.Pan()
    .onUpdate((e) => {
      translateX.value = e.translationX;
      translateY.value = e.translationY;
    })
    .onEnd(() => {
      translateX.value = withSpring(0);
      translateY.value = withSpring(0);
    });

  const style = useAnimatedStyle(() => ({
    transform: [
      { translateX: translateX.value },
      { translateY: translateY.value },
    ],
  }));

  return (
    <GestureDetector gesture={pan}>
      <Animated.View style={[styles.box, style]} />
    </GestureDetector>
  );
}
4. 布局动画
import Animated, { FadeIn, FadeOut, Layout } from 'react-native-reanimated';

function AnimatedList({ items }) {
  return (
    <Animated.View layout={Layout.springify()}>
      {items.map((item) => (
        <Animated.View
          key={item.id}
          entering={FadeIn}
          exiting={FadeOut}
          layout={Layout.springify()}
        >
          <Text>{item.title}</Text>
        </Animated.View>
      ))}
    </Animated.View>
  );
}
5. 动画时序
函数使用场景
withTiming线性动画,时长可控
withSpring自然的、基于物理效果的动画
withDecay基于惯性的动画(甩动)
withSequence按顺序播放多个动画
withRepeat循环播放动画

快速入门示例

import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
  interpolate,
} from 'react-native-reanimated';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';

function SwipeCard() {
  const translateX = useSharedValue(0);

  const gesture = Gesture.Pan()
    .onUpdate((e) => { translateX.value = e.translationX; })
    .onEnd(() => { translateX.value = withSpring(0); });

  const style = useAnimatedStyle(() => ({
    transform: [
      { translateX: translateX.value },
      { rotate: `${interpolate(translateX.value, [-200, 200], [-15, 15])}deg` },
    ],
  }));

  return (
    <GestureDetector gesture={gesture}>
      <Animated.View style={[styles.card, style]} />
    </GestureDetector>
  );
}

常见错误与解决方案

错误原因解决方案
"Attempted to call from worklet"缺少 runOnJS使用 runOnJS() 包装
动画未运行缺少 'worklet'添加 'worklet' 指令
手势不起作用缺少根视图添加 GestureHandlerRootView

验证清单

  • 动画以 60fps 运行
  • 手势响应流畅
  • 低端设备上没有掉帧
  • 布局动画不会造成卡顿

用法

Skill("react-native-animations")

绑定的 Agent:05-react-native-animation

评论

支持 Markdown