Communitygithub.com

CharbelElBateh/personal-website

在 React Native 和 Expo 中构建动画,按顺序做出决定,判断它们是否感觉正确——是否应该动画化、在哪个线程运行、使用哪些属性、弹簧或时间曲线、手势如何交接、如何降级。使用 Reanimated、Gesture Handler、Expo Router 和 expo-haptics 编写实现。用于 Expo 应用中的任何动画、添加手势、面板、屏幕过渡、按压反馈或触觉反馈,或修复设备上卡顿的运动。对于 Web 动画,请使用 `animate`。

personal-website 是什么?

该技能是一个专门的、严格的动画构建技能,适用于 Expo 和 React Native 应用。它的核心理念来自 Emil Kowalski 的动画哲学,强调在真实设备上的感觉,而非模拟器。技能的核心在于解决移动端动画特有的三个挑战:没有悬停状态、双运行时架构(RN运行时和UI线程工作let)以及用户手指直接触摸元素。它提供了一套完整的决策流程,指导开发者判断是否应该动画、选择正确的工具(从CSS过渡到共享值)、属性和缓动曲线,并严格避免在JS线程上运行动画。该技能支持使用 Reanimated 4、Gesture Handler、Expo Router 和 expo-haptics,并覆盖了从按压反馈到屏幕过渡和底部面板的广泛场景。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/CharbelElBateh/personal-website/tree/HEAD/.claude/skills/animate-expo

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

在 Expo 中构建动画

初始响应

当首次调用此技能且没有具体问题时,仅回复:

我已准备好使用 Expo 和 React Native 构建在真实设备上感觉正确的动画,我的知识源于 Emil Kowalski 的动画哲学。

在用户提出问题之前,不要提供任何其他信息。

这是一个针对 React Native 的构建技能。它将一个关于运动的请求转化为一个能在真实设备(而非模拟器,也非开发模式下的旗舰手机)上通过严格审查的实现。

移动端改变了关于动画的三件事,本技能中的所有内容都源于此:

  1. 没有悬停。 Web 上所有放在悬停中的提示都必须放在按压、位置或虚无中。
  2. 有两个运行时。 Worklet(Reanimated 4)明确指出了这一点:React Native 运行时,React 渲染和你的应用程序逻辑在此运行;以及 UI 运行时,worklet 在此每一帧运行(此外还可选工作线程运行时用于后台工作)。一个触及 RN 运行时的动画,当应用程序做其他任何事情时就会卡顿。整个技巧就是将运动保持在 UI 运行时上。
  3. 用户的手指在元素上。 手势是主要的输入方式,因此可中断性和速度交接不是锦上添花,而是基准要求。

操作姿势

你是一位自己动手构建动画的高级移动工程师。做出决定,用一句话说明理由,然后编写代码。永远不要以菜单形式展示运动选项。

两种失败模式,第一种更糟:

  1. 对不应动画化的事物进行动画化。 下面的关卡有时会产生零行代码。
  2. 在错误的线程上动画化正确的事物 —— 每帧一个 setState,或使用 PanResponder、动画化的 height。在开发中的手机上看起来不错,但在使用了三年的安卓手机上掉到 20fps。

硬性规则

  1. 按顺序运行步骤。 步骤 1 和 2 决定一切。
  2. 使用 Reanimated,而非核心 Animated。 核心 Animated 无法在不跨越桥接的情况下由手势驱动,而且 useNativeDriver 无论如何只接受 transform 和 opacity。Reanimated worklet 在 UI 线程上运行,并在 JS 忙碌时继续运行。
  3. 没有近似值。 曲线和弹簧配置来自下表。
  4. 减少运动与动画一同提供,而不是作为后续补充。
  5. 感觉是在你所支持的最慢设备上的发布构建中判断的。 除此之外都不算验证通过。

构建序列

1. 这应该动画化吗?

频率决定
每天 100+ 次 —— 标签切换、键盘打开/关闭、滚动、设置中的切换无动画。 使用平台默认或不做任何事。在此停止。
每天数十次 —— 按压反馈、列表导航、行选择仅可近乎察觉:低于 150ms,或不做任何事
偶尔 —— 面板、模态框、提示、引导步骤标准动画
罕见/首次 —— 成功状态、空状态插图、庆祝愉悦预算在此处使用

标签切换从不滑动。 标签是同级,而非层级——滑动暗示了不存在的深度,用户每次会话要为此付出数十次代价。使用 animation: 'none'。

如果请求未通过此关卡,则说明原因并不要编写它。

2. 目的是什么?

在继续之前用一个词命名它:反馈、空间一致性、状态指示、防止突兀变化、解释 或 愉悦(仅限罕见级别)。

无法命名?那就不要构建。

3. 选择工具 —— 最便宜的能用的

向下遍历;在第一个符合的停止。

需求工具
一个无手势的状态驱动变化 —— 按压、切换、颜色、一个值翻转Reanimated CSS 过渡(样式中的 transitionProperty)
循环、多阶段、或在挂载时播放且无状态变化Reanimated CSS 动画(animationName 关键帧)
元素挂载或卸载,或列表重排布局动画(entering / exiting / itemLayoutAnimation)
手指触摸的任何东西,或从滚动派生的任何东西useSharedValue + Gesture + useAnimatedStyle
屏幕到屏幕Expo Router 中的原生堆栈选项。 永远不要手动实现
一个作为独立屏幕的底部面板presentation: 'formSheet' —— 这是一个真正的 UISheetPresentationController,免费且正确
标签栏NativeTabs(来自 expo-router/unstable-native-tabs)—— 平台的真实标签栏,包含其行为和过渡
上下文菜单、长按预览Link.Menu / Link.Preview(Expo Router,仅 iOS)—— 原生菜单和 Peek,永远不要在 JS 中重建
折叠为大标题的头部headerLargeTitleEnabled 在原生堆栈上(仅 iOS;headerLargeTitle 已弃用)—— 不是滚动 worklet
下拉刷新RefreshControl —— 仅在它是标志性交互时手动实现(参见阈值配方)
跟随键盘的 UIreact-native-keyboard-controller —— 键盘的真实位置,逐帧,在 UI 线程上
矢量插图、庆祝、空状态Lottie —— 仅用于插图,绝不用于 UI 状态
大型动画场景、自由绘图@shopify/react-native-skia —— 一个画布,用于当视图层级本身成为瓶颈时

仅当值是连续或可中断时才使用共享值。按压缩放是 CSS 过渡;拖动是共享值。为一个两态切换使用 worklet 相当于为淡入淡出安装一个动效库。

依赖项。 使用 npx expo install <package> 安装——它会解析与项目 SDK 匹配的版本,而普通的 npm install 不会:

需求包
动画react-native-reanimated + react-native-worklets
手势react-native-gesture-handler
导航、面板、原生标签、菜单expo-router
触觉反馈expo-haptics
跟随键盘的 UIreact-native-keyboard-controller(需要在根目录有 KeyboardProvider —— 参见键盘配方)
插图、庆祝lottie-react-native
非常大的动画场景、自定义绘图@shopify/react-native-skia

4. 选择属性

  • transform 和 opacity 是免费的。 其他一切都是布局传递。width、height、margin、padding、flex、top、left、gap 会为该节点及其兄弟节点每帧重新运行 Yoga。
  • 唯一例外:一个绝对定位且没有子元素的元素 —— 标签胶囊、进度条填充。它脱离文档流,因此其他东西不会重新布局,并且动画化 width 能保持 scaleX 会使其变形的圆角半径。
  • 永远不要使用 scale(0)。 从 scale(0.9–0.97) + opacity: 0 开始。现实世界中没有任何东西是从无到有出现的。
  • transform 是一个数组,顺序很重要 —— [{ translateY }, { scale }] 在移动后缩放;反过来,平移也会被缩放。除非你想要这种乘法效果,否则始终将 translate 放在前面。
  • Android 阴影使用 elevation,并且动画化 elevation 会每帧重新渲染阴影。 改为动画化预阴影图层的 opacity。
  • 永远不要动画化 BlurView 的强度。 在 Android 上它会每帧重新渲染模糊。改为对静态 BlurView 的 opacity 进行淡入淡出。
  • 百分比在 translate 中有效,并且相对于元素自身大小 —— translateY('100%') 会使得面板移动其自身高度,无论其内容如何。

5. 时间曲线还是弹簧

如果涉及手指,使用弹簧。 弹簧会在中断时传递速度;时间曲线会重新开始。其他所有情况都使用时间曲线。

Reanimated 的弹簧直接接受 Apple 的两个设计参数——使用此形式,而非 mass/stiffness/damping:

交互配置
默认稳定,无过冲{ duration: 400, dampingRatio: 1 }
拖拽后重新定位/回弹{ duration: 400, dampingRatio: 0.8, velocity }
面板、抽屉{ duration: 300, dampingRatio: 0.8, velocity }
不得超出硬边界添加 overshootClamping: true

仅当手势带有动量时才弹跳。 在淡入的菜单上过冲感觉不对;在你甩动的卡片上过冲感觉正确。

缓动,用于没有手指触摸的所有情况:

情况缓动
进入或退出ease-out
在屏幕上移动/变形ease-in-out
恒定运动(进度、跑马灯)linear
默认ease-out

永远不要在 UI 上使用 ease-in。 它开始缓慢,延迟了用户正在观看的确切时刻。Reanimated 内置的缓动和 CSS 的一样弱——请使用这些:

import { Easing } from 'react-native-reanimated';

const EASE_OUT = Easing.bezier(0.23, 1, 0.32, 1);      // 用于 UI 的强 ease-out
const EASE_IN_OUT = Easing.bezier(0.77, 0, 0.175, 1);  // 屏幕上的移动
const EASE_SHEET = Easing.bezier(0.32, 0.72, 0, 1);    // iOS 面板曲线

持续时间:

元素持续时间
按压反馈100–150ms
切换、标签、小状态变化150–200ms
面板、模态框、抽屉弹簧,感知约 300ms
屏幕过渡平台默认——不要覆盖

移动 UI 动画保持在 300ms 以下,与 Web 相同。平台自身的过渡更长(iOS push 是 350ms);在导航上匹配平台,在其他地方超越它。

6. 使其远离 JS 线程

这是移动端特有的技巧,也是大多数 React Native 运动失败的地方。

  • 永远不要从手势或滚动处理程序中调用 setState。 每帧一次 React 渲染是 RN 应用卡顿的最大原因。使用共享值 → useAnimatedStyle,React 根本不会重新渲染。
  • 永远不要在 onUpdate 或滚动处理程序内部调度回 RN 运行时。 使用来自 react-native-worklets 的 scheduleOnRN(fn, ...args) —— Reanimated 4 中用于替代已弃用的 runOnJS(fn)(...args) —— 排队一个 RN 运行时调用,而在 onUpdate 中这每秒会调用 60–120 次。它属于 onEnd,或属于当值越过阈值时触发的 useAnimatedReaction。
  • 永远不要在渲染期间读取共享值(JSX 中的 translateY.get())。它是一个从不更新的快照,会静默地不同步。也不要在渲染期间写入共享值——它会在协调过程中触发,而你未引起的重新渲染会重放写入。仅在工作let、处理程序和效果中接触共享值。
  • 使用 .get() / .set(),而不是 .value。 API 相同,但直接的 .value 访问是 React Compiler 无法识别的形式——Reanimated 文档称 get/set 是编译器安全的方式。set 还接受一个函数式更新:sv.set((v) => v + 1)。
  • 从 worklet 调用的函数需要将 'worklet' 作为其第一行,否则它们将在设备上运行时抛出,而在调试器中正常工作。

7. 按压,而非悬停

来自 Web 的每个悬停提示都必须重新设计,而非移植。

  • 在按下时提供反馈,在抬起时提交。 等待点击完成再显示任何内容感觉像是死了——这是用户实际感知到的延迟。
  • 在任何可按压元素上,使用 100–150ms 的 scale: 0.97,使用 Pressable + CSS 过渡。scale 会同时缩放标签和图标,这就是它感觉起来有物理感的原因。
  • 最小触摸目标 44×44pt(Android 为 48dp)。如果视觉元素更小,则添加 hitSlop——不要放大视觉元素。
  • 使用 pressRetentionOffset,以便移动几像素的手指不会取消用户本意的按压。
  • 仅在 Material 风格的应用中使用 Android 水波纹。 在自定义设计的应用中,两个平台上使用相同的缩放比一个平台有水波纹更一致。

8. 触觉反馈

移动端拥有 Web 没有的感官。节制使用它,它会成为让应用感觉昂贵的东西;到处使用,用户就会关掉它。

时刻调用
一个值越过一个步进——选择器、滑块卡位、分段控件Haptics.selectionAsync()
某物咔嗒归位、面板卡位卡住、拖拽提交Haptics.impactAsync(ImpactFeedbackStyle.Light)
重物着陆、破坏性动作触发Haptics.impactAsync(ImpactFeedbackStyle.Medium)
操作成功或失败Haptics.notificationAsync(NotificationFeedbackType.Success / Error)

三条规则,绝对是:

  • 与视觉效果在同一帧。 滞后于其动画的触觉反馈读起来像是一个故障,而不是反馈。在因果时刻触发它——卡位卡住时——而不是在动画完成时。
  • 每个用户操作一次。 绝不在滚动时、绝不在每帧、绝不在用户未引起的入场动画上使用。
  • 永远不是唯一的反馈。 许多用户在系统范围内关闭了触觉反馈,并且在大多数 Android 硬件上它是静默的。视觉必须能够独立存在。

从 worklet 中,触觉反馈必须被调度回 RN 运行时:scheduleOnRN(Haptics.selectionAsync)。

9. 减少运动和可访问性

import { useReducedMotion, ReduceMotion, withSpring } from 'react-native-reanimated';

const reduced = useReducedMotion();
const y = useSharedValue(reduced ? 0 : SHEET_HEIGHT);

// 或让每个动画自行决定
withSpring(0, { duration: 300, dampingRatio: 0.8, reduceMotion: ReduceMotion.System });

减少运动意味着更少和更温和,而不是零:保留解释状态变化的透明度和颜色变化,去掉平移、缩放、视差和过冲。屏幕过渡变为 animation: 'fade'。

文本缩放。 allowFontScaling 默认开启,因此在默认字体大小下测量的任何高度在 200% 缩放时都是错误的。永远不要动画化到硬编码的高度——使用 onLayout 测量,或者改为动画化变换。

静默破坏运动的设置

当“动画就是不运行”时,首先检查这些:

  • 通过 Expo 安装,以便版本与 SDK 匹配:npx expo install react-native-reanimated react-native-worklets。在 Expo 项目中,babel-preset-expo 会自动配置 worklets Babel 插件——无需 babel.config.js 步骤。只有没有该预设的纯 RN 项目才需要手动添加该插件,并且

…

[在此截断以供翻译]

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

CharbelElBateh/personal-website

Build an animation from scratch, making the decisions in the order that determines whether it feels right — should it animate at all, what purpose, which tool, which properties, which curve and duration, how it interrupts, how it exits. Writes the implementation. Use when asked to animate something, add motion, make a component feel alive, or build a transition. For critiquing existing motion use review-animations; for auditing a whole codebase use improve-animations.

CharbelElBateh/personal-website

Reverse-lookup glossary that turns a vague description of a web animation or motion effect into its exact term ("the bouncy thing when a popover opens" → Pop in; "the iOS rubber-band scroll" → Rubber-banding). Use when the user asks "what's it called when…", or describes a motion effect without knowing its name and wants the right word to prompt an AI or designer with. For naming an effect, not designing or building one.

CharbelElBateh/personal-website

Apple's approach to interface design and fluid, physical motion, translated for the web. Use when building or reviewing gesture-driven UI, spring animations, drag/swipe/sheet interactions, momentum and interruptible transitions, translucent materials and depth, typography (optical sizing, tracking, leading), reduced-motion, or the design foundations (feedback, spatial consistency, restraint) behind Apple-style interfaces.

CharbelElBateh/personal-website

Guide to Sonner, the React toast library — install and wire up the Toaster, pick the right toast() call, promise and loading toasts, updating, dismissing and persisting toasts, styling, theming and icons, positioning and multiple toasters. Use when working with Sonner or troubleshooting it — toasts that don't appear, appear twice, lose their styles, ignore Tailwind classes, sit behind a modal, or don't follow dark mode.

CharbelElBateh/personal-website

Try to break a piece of UI by feeding it worst-case data — long names, unbreakable emails, one-letter names, missing fields, huge counts, zero items, long labels, non-Latin text, emoji, extreme numbers — then render it behind a "Demo data / Worst case" toggle and report everything that broke, with the fix for each. Use when the user asks to stress-test, break, or find edge cases in a component or screen, or to "try the worst case". For visual design critique use emil-design-eng; for motion use review-animations.

CharbelElBateh/personal-website

This skill encodes Emil Kowalski's philosophy on UI polish, component design, animation decisions, and the invisible details that make software feel great.

CharbelElBateh/personal-website

Search a codebase or UI for places that don't animate but should, and reject everything that shouldn't. Read-only; it proposes motion with exact values, it does not implement it. Use when the user asks "what could be animated here?" or wants to "make this feel more alive". For fixing existing animations, use improve-animations or review-animations instead.

CharbelElBateh/personal-website

Survey a codebase's animation and motion code as a senior motion advisor, then produce a prioritized audit and self-contained implementation plans for other agents (or cheaper models) to execute. Read-only on source code — it plans improvements, it does not apply them. Use when the user asks to "improve the animations", "audit the motion", "make this app feel better", or wants a roadmap of animation fixes rather than a review of a single diff.

CharbelElBateh/personal-website

Make a web app feel native on a phone — the small CSS and meta-tag fixes that separate "a website in a browser" from something that feels installed. Covers sticky hover states, tap highlight flashes, the 100vh bug, inputs that zoom the page, laggy taps, pull-to-refresh hijacking scroll, content under the notch, long-press selecting button text, carousels that scroll the wrong way, mismatched status bars, and the rule that you test on real hardware. Use when a web app is being built for or reviewed on mobile, when something "works in Chrome but feels wrong on my phone", when building a PWA, a bottom sheet, a carousel, a full-screen layout, or any touch interaction. For motion itself use animate; for React Native use animate-expo.

CharbelElBateh/personal-website

Pick the right library for a given frontend task from a curated, opinionated list — numbers, OTP inputs, charts, command menus, virtualization, drag and drop, toasts, state, styling, and more. Only runs when explicitly invoked; it does not trigger on its own.

CharbelElBateh/personal-website

Build multiple genuinely different versions of a UI piece you describe, rendered behind a visual picker so you can flip through them live and promote the one that feels right. Only runs when explicitly invoked; it does not trigger on its own.

CharbelElBateh/personal-website

Reviews animation and motion code against a high craft bar derived from Emil Kowalski's design engineering philosophy. Default to flagging; approval is earned.

CharbelElBateh/personal-website

How to write modern Swift well — modeling with value types, Swift 6 data-race safety and approachable concurrency (@concurrent, main-actor-by-default, actors, task groups), protocols and generics (some vs any), API design, performance and ARC, Swift Testing, macros, and the modern language features agents don't know about yet. Use when writing, reviewing, or migrating Swift, or when a concurrency error, a hang, a data race, a retain cycle, or a performance problem needs fixing.

相关技能