淡入/淡出 ScrollView Swift UI

2024-03-14

当用户滚动文本时,是否可以淡入/淡出滚动视图顶部和底部的文本? Xcode 14.2、iOS 16、Swift 5.7

我研究了其他解决方案,例如:SwiftUI - 淡出 ScrollView https://stackoverflow.com/questions/63873033/swiftui-fade-out-a-scrollview

我尝试过以下操作,但它水平淡出侧面并添加颜色。我希望它是垂直的并且透明淡入淡出,如图所示。

ScrollView {
    ScrollViewReader { scrollViewProxy in
        VStack {        
            ForEach(chatMessages, id: \.id) { message in
                messageView(message: message)
                
            }
            .mask(
                VStack(spacing: 0) {
                    
                    // Top gradient
                    LinearGradient(gradient:
                                    Gradient(
                                        colors: [Color.black.opacity(0), Color.black]),
                                   startPoint: .leading, endPoint: .trailing
                    )
                    .frame(width: 250)
                    
                    // Middle
                    Rectangle().fill(Color.black)
                    
                    // Bottom gradient
                    LinearGradient(gradient:
                                    Gradient(
                                        colors: [Color.black, Color.black.opacity(0)]),
                                   startPoint: .leading, endPoint: .trailing
                    )
                    .frame(width: 250)
                }
            )   
        }
    }
}

我已经使用此代码来产生水平淡入淡出,但需要在顶部和底部垂直。这是模拟器中现在的内容。

看图片:


这可以通过使用Rectangle()使用“移除”顶部和底部的面罩.blendMode(.destinationOut)

这允许您在顶部和底部有固定的高度梯度。

struct ContentView: View {
    @State var nameTextField: String = ""
        
    var body: some View {
        ScrollView {
            Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n").bold()
        }
        .mask {
            Rectangle()
                .overlay(alignment: .top) {
                    ScrollMask(isTop: true)
                }
                .overlay(alignment: .bottom) {
                    ScrollMask(isTop: false)
                }
        }
        .padding()
        .foregroundColor(.white)
        .scrollContentBackground(.hidden)
        .background {
            Image("wave")
        }
    }
}

struct ScrollMask: View {
    let isTop: Bool

    var body: some View {
        LinearGradient(colors: [.black, .clear], startPoint: UnitPoint(x: 0.5, y: isTop ? 0 : 1), endPoint: UnitPoint(x: 0.5, y: isTop ? 1 : 0))
            .frame(height: 150)
            .frame(maxWidth: .infinity)
            .blendMode(.destinationOut)
    }
}

另一种方法是使用单个LinearGradient具有多种颜色/停止点。在这种情况下,顶部三分之一淡入,底部三分之一淡出:

.mask {
    LinearGradient(colors: [.clear, .black, .black, .clear],
                   startPoint: UnitPoint(x: 0.5, y: 0), endPoint: UnitPoint(x: 0.5, y: 1))
        .frame(maxWidth: .infinity, maxHeight: .infinity)
}

See here https://www.fivestars.blog/articles/reverse-masks-how-to/有关反向蒙版的更多信息

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

淡入/淡出 ScrollView Swift UI 的相关文章

随机推荐

  • 从内存缓冲区初始化 C++ std::istringstream?

    我有一个内存块 不透明 我想通过他们的 C 适配器将其存储在 mySQL 中的 Blob 中 适配器需要一个 istream virtual void setBlob unsigned int parameterIndex std istr
  • 浏览器的HTTP缓存的存储位置在哪里?磁盘或内存

    浏览器的HTTP缓存的存储位置在哪里 磁盘或内存 我只是想知道为什么有些文件来自磁盘缓存而另一些文件来自内存缓存 HTTP 缓存背后的机制是什么 哪个缓存的优先级更高 看起来脚本和样式表存储在磁盘中 而图像和字体存储在内存中 也许是解决方案
  • Django 查询选择不同的字段对

    我的 提交 字段有一个用户和一个问题 如何获得 SQL 搜索结果 该结果将给出每个用户 问题对仅包含一个结果的列表 模型是这样的 class Problem models Model title models CharField Title
  • 使用 CSS 3D 变换从 DOM 对象接收变换后的鼠标事件数据

    目前 JavaScript 鼠标事件中是否有任何数据可以让我轻松查找或计算相对于转换元素的 3D 空间的鼠标位置 为了直观地说明 左边是没有 3d 矩阵的 div 右边是经过 3d 变换的 div o是鼠标事件的起源 o gt o 在下面的
  • Python - Pandas 删除 Excel 中的特定行/列

    我有以下 Excel 文件 我想清理特定的行 列 以便我可以进一步处理该文件 我已经尝试过这个 但我没有设法删除任何空白行 我只能设法从包含数据的行中修剪掉 在这里 我试图只保存第三行及以后的数据 xl pd ExcelFile MRD x
  • TemplateInputException:解析模板时出错 ****,模板可能不存在

    我正在使用 Spring Boot 和 mySql 数据库 我正在尝试通过团队表过滤搜索 最初这个表是一个 thymeleaf 表 参见我的相关 SO 问题 但在用尽互联网上可用的所有资源后 没有成功 我决定以不同的方式实现搜索 我发现了一
  • 通过 C# 和 OleDb 将行插入 Excel 电子表格

    我需要以编程方式将一行多次插入 Excel 电子表格中 我需要实际上插入一个新行并且不插入数据 也就是说 我实际上需要将所有其他行向下移动一位 我目前正在使用 OleDB 插入数据本身 如下所示 Note I have missed som
  • 写组合缓冲区位于哪里? x86

    Write Combine 缓冲区在物理上是如何连接的 我见过说明许多变体的框图 L1 和内存控制器之间 CPU 的存储缓冲区和内存控制器之间 CPU 的 AGU 和 或存储单元之间 它依赖于微架构吗 写缓冲区在不同的处理器中可以有不同的目
  • 如何将 iPhone 应用程序从 Xcode 部署到真实的 iPhone 设备?

    如果没有 99 美元的 Apple 证书 如何将 iPhone 应用程序从 Xcode 部署到真实的 iPhone 设备 I ve used a mix of two howtos Jason s https stackoverflow c
  • Couchbase 的 Twisted API 无法与 Python Tornado 一起使用

    我正在尝试使用 Couchbase 4 0 开发人员预览版运行 Tornado 服务器 import tornado web import tornado httpserver import tornado options import t
  • Apache HttpClient 和自定义端口

    我正在使用 Apache HttpClient 4 它工作正常 唯一不起作用的是自定义端口 似乎提取了根目录而忽略了端口 HttpPost post new HttpPost http myserver com 50000 HttpResp
  • 为什么通过translate3d改变div大小时会出现边距?

    当我更改 div 大小时 块之间会出现缩进 div class test2 test2 p hello p p hello p div 帮我看看怎么去掉 container perspective 600px transform style
  • 尝试使用 Selenium 循环配置文件列表

    我试图循环遍历所有个人资料并将人员姓名 工作资料和位置存储在列表中 这是我所在的 LinkedIn 屏幕的屏幕截图 这是我必须循环的 li html 标签 li class reusable search result container
  • 如何为多个AWS EC2实例设置共享持久存储?

    我有一项托管在 Amazon Web Services 上的服务 我有多个 EC2 实例运行着完全相同的设置和数据 由弹性负载均衡器和扩展组管理 这些实例是运行基于 PHP 的 Web 应用程序的 Web 服务器 因此 目前每个实例上都放置
  • 包含主 *.tex 文件的超级/子目录中的 *.sty 文件

    我想通过 git 与许多其他人分享乳胶文档 因此 我们决定将每个人的 Latex 安装中不存在的所有特殊 sty 文件放入资源目录中 如果这个目录是超级目录 那就太酷了 实际工作目录的 我究竟如何导入这些样式文件 重要的是 即使这些远程样式
  • 可用于测试 JavaScript 验证脚本的电子邮件地址列表

    有人有我可以用来测试 JS 地址验证脚本的电子邮件地址列表吗 我正在寻找尽可能完整的列表来测试最常见的边缘情况 如果不是所有情况 根据 RFC2822 有效的示例 电子邮件受保护 cdn cgi l email protection 电子邮
  • 有什么方法可以将 Rstudio 中的颜色更改为默认选项以外的颜色吗?

    有没有办法改变配色方案 for Rstudio 它配备了6 or 7默认 预先打包的主题 我想知道是否有任何东西可以让我自定义这些或一些可以让我自己制作的插件 有一种更快的方法可以解决这个问题 而且 100 可行 使用您最喜欢的编辑器主题打
  • 值类型后面的问号的用途是什么(例如:int?myVariable)?

    通常问号的主要用途是用于条件 x yes no 但我看到了它的另一种用途 但找不到这种用途的解释 例如 运算符 public int myProperty get set 这意味着所讨论的值类型是可空类型 https msdn micros
  • 如何通过我的切换帧函数将 Tkinter 条目值从一个帧传递到另一个帧

    我的 Tkinter 应用程序添加了笔记本 在笔记本内我想使用按钮切换框架 实现了笔记本开关和框架开关 当我单击 确定 按钮时 我想将笔记本的一帧输入输入到另一帧enter code here 我尝试将值作为框架类初始化的参数传递 将输入字
  • 淡入/淡出 ScrollView Swift UI

    当用户滚动文本时 是否可以淡入 淡出滚动视图顶部和底部的文本 Xcode 14 2 iOS 16 Swift 5 7 我研究了其他解决方案 例如 SwiftUI 淡出 ScrollView https stackoverflow com q