You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native:@gorhom/bottom-sheet打开时背景容器动画实现求助

实现@gorhom/bottom-sheet v4背景容器层叠倾斜动画

@gorhom/bottom-sheet v4本身没有内置实现这种主容器倾斜层叠动画的props,但可以利用组件暴露的动画状态值自己实现,下面是具体的实现思路和代码示例:

核心思路

组件提供了animatedPosition(或animatedIndex)这个Animated.Value,它会随着BottomSheet的滑动/展开实时更新:

  • 值为0时代表BottomSheet完全关闭
  • 值为1时代表BottomSheet展开到第一个锚点
  • 多锚点场景下,值会对应不同的展开比例

我们可以用这个值来驱动主应用容器的缩放、旋转、位移、透明度等变换,模拟出类似Slack的层叠倾斜效果。

代码示例

import React, { useRef } from 'react';
import { View, Animated, StyleSheet } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';

const YourAppComponent = () => {
  const bottomSheetRef = useRef(null);
  // 获取BottomSheet的动画位置值
  const animatedPosition = bottomSheetRef.current?.animatedPosition;

  // 计算主容器的动画样式
  const animatedContainerStyle = {
    transform: [
      // 缩放效果:从1缩小到0.95
      {
        scale: animatedPosition?.interpolate({
          inputRange: [0, 1],
          outputRange: [1, 0.95],
          extrapolate: 'clamp',
        }) || 1,
      },
      // 旋转Y轴:从0度转到-3度,模拟倾斜
      {
        rotateY: animatedPosition?.interpolate({
          inputRange: [0, 1],
          outputRange: ['0deg', '-3deg'],
          extrapolate: 'clamp',
        }) || '0deg',
      },
      // 水平位移:向左偏移10
      {
        translateX: animatedPosition?.interpolate({
          inputRange: [0, 1],
          outputRange: [0, -10],
          extrapolate: 'clamp',
        }) || 0,
      },
    ],
    // 透明度:从1降到0.8
    opacity: animatedPosition?.interpolate({
      inputRange: [0, 1],
      outputRange: [1, 0.8],
      extrapolate: 'clamp',
    }) || 1,
  };

  // BottomSheet锚点配置
  const snapPoints = ['30%', '60%'];

  return (
    <View style={styles.appContainer}>
      {/* 主应用容器:用Animated.View包裹以实现动画 */}
      <Animated.View style={[styles.mainContent, animatedContainerStyle]}>
        {/* 这里放入你的主应用内容 */}
      </Animated.View>

      {/* BottomSheet组件 */}
      <BottomSheet
        ref={bottomSheetRef}
        snapPoints={snapPoints}
        // 其他你需要的props,比如backgroundStyle、handleStyle等
      >
        {/* BottomSheet内容 */}
      </BottomSheet>
    </View>
  );
};

const styles = StyleSheet.create({
  appContainer: {
    flex: 1,
  },
  mainContent: {
    flex: 1,
    // 主内容的基础样式,比如背景色、padding等
  },
});

export default YourAppComponent;

调整建议

  • 你可以修改interpolate的outputRange数值,比如调整旋转角度(-3deg改成-5deg)、缩放比例(0.95改成0.92),来更贴近Slack的动画效果
  • 如果需要更细腻的动画,可以结合Animated.parallel或者添加Y轴位移,让效果更有层次感
  • 若使用animatedIndex替代animatedPosition,inputRange需要对应锚点的索引(比如[0,1]对应关闭和第一个锚点)

Slack移动端BottomSheet背景层叠动画效果

内容的提问来源于stack exchange,提问作者amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:05:36