AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79323092
Accepted
financial_physician
financial_physician
Asked: 2025-01-02 15:52:19 +0800 CST2025-01-02 15:52:19 +0800 CST 2025-01-02 15:52:19 +0800 CST

模仿在圆边纸张上滚动

  • 772

我正在 React Native 中实现一个模态框,并希望创建一个独特的滚动效果,其中模态框具有圆角,可根据滚动位置动态调整。具体来说,我希望模态框看起来像一个可滚动的页面,两端都有圆角 - 查看内容顶部时,顶角应该是圆的;查看底部时,底角应该是圆的;滚动中间部分时,角应该是方形的。这会产生类似于通过窗口查看圆形卡片的效果,其中圆边仅在该部分可见时可见。目标是保持圆形卡片的视觉隐喻,同时提供滚动位置的清晰指示。

所以目前我有类似的东西:

<View style={{borderRadius: 8}}>
  <ScrollView>
     // ... some content
  </ScrollView>
</View>

它的角总是圆的,但如果只在顶部和底部圆,那就太酷了……这似乎可能是一个标准效果,但我以前没有做过太多的前端

react-native
  • 1 1 个回答
  • 22 Views

1 个回答

  • Voted
  1. Best Answer
    Dylan
    2025-01-02T16:35:52+08:002025-01-02T16:35:52+08:00

    我建议让整个模式可滚动,并保持模式主体的角半径,但要回答你的问题,如何通过从上到下滚动使角半径动态化,这是我的答案

    1. 你需要使用 来聆听你的ScrollView卷轴onScroll
    2. 检查你的 ScrollView 的 y 偏移量是在顶部还是底部

    这是我的示例代码

    
    const TestScreen = () => {
      const [borderRadiusStyle, setBorderRadiusStyle] = useState<ViewStyle>(styles.roundedTop);
    
      const handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
        const heightBeforeRound = 0 // adjust this if you want your get your corner radius before reaching the tip of the scroll
    
        const offsetY = event.nativeEvent.contentOffset.y;
        const height = event.nativeEvent.contentSize.height
        const viewHeight = event.nativeEvent.layoutMeasurement.height
        
        if (offsetY <= heightBeforeRound) {
            setBorderRadiusStyle(styles.roundedTop)
        } else if (offsetY + viewHeight >= height - heightBeforeRound) {
            setBorderRadiusStyle(styles.roundedBottom)
        } else {
            setBorderRadiusStyle(styles.square)
        }
      };
    
      return (
        <View style={[styles.container]}>
          <ScrollView 
            contentContainerStyle={styles.scrollViewContent} 
            onScroll={handleScroll}
            scrollEventThrottle={16} // Adjust throttle for smoother updates
          >
            {/* Add your content here */}
        </View>
      );
    };
    
    const cornerRadius = 8 // adjust on how much corner radius do you want
    const styles = StyleSheet.create({
      container: {
        flex: 1
      },
      roundedTop: {
        borderTopLeftRadius: cornerRadius,
        borderTopRightRadius: cornerRadius,
        borderBottomLeftRadius: 0,
        borderBottomRightRadius: 0,
      },
      roundedBottom: {
        borderTopLeftRadius: 0,
        borderTopRightRadius: 0,
        borderBottomLeftRadius: cornerRadius,
        borderBottomRightRadius: cornerRadius,
      },
      square: {
        borderRadius: 0,
      },
      scrollViewContent: {
        padding: 16,
      },
    });
    

    这是我的解释

    1. 我用来event.nativeEvent.contentOffset.y获取偏移量 y,这样我们就知道你滚动到了哪里
    2. 我用height和viewHeight来知道你是否在底部
    3. 我使用条件来检查你是在底部还是在顶部,否则意味着你在中心,所以我把它变成正方形

    注意:由于我使用 typescript,因此请导入这些类型,如果你只使用 javascript,请删除它们

    • 1

相关问题

  • 从同级组件访问 DrawerStatus

  • 反应本机按钮不响应样式表更改(居中按钮)

  • 反应原生元素输入边框底部问题,​​带半径

  • 仅当拉动发生在 FlatList 组件周围时,拉动刷新才有效

  • 电子邮件未发送到 *.js 中的用户电子邮件地址

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve