【问题标题】:SwiftUI endless gradient backgroundSwiftUI 无尽渐变背景
【发布时间】:2020-08-19 21:59:22
【问题描述】:

我想制作一个带有渐变背景的滚动应用。随着用户滚动 - 背景颜色发生变化。 例如,底部为黑色,顶部为白色,我将指定 VStack 的高度为 8000,对于这个高度,当用户滚动屏幕时,他会看到颜色变化。

我没有找到任何解决方案。尝试为 VStack 和 Rectangle 图形制作全高的 LinearGradient,但它只覆盖手机屏幕尺寸(不能填充所有 8000 个高度点)。所以如果我向上滚动,渐变会丢失,它只显示黑屏。

谢谢]1

【问题讨论】:

    标签: xcode uiscrollview swiftui scrollview linear-gradients


    【解决方案1】:

    您可以使用 ZStack 中的矩形来实现此目的:

    struct ContentView: View {
        var body: some View {
            ScrollView {
                ZStack(alignment: .top) {
                    // Example width and height. Width should be the width of the device
                    Rectangle().frame(width: 10000, height: 2000).foregroundColor(.clear).background(LinearGradient(gradient: Gradient(colors: [.white, .black]), startPoint: .top, endPoint: .bottom))
                    // Add your actual content here:
                    Text("Yeet")
                }
            }
        }
    }
    

    【讨论】:

    • 这完全适用于默认的 ScrollView,谢谢!我忘了提。我正在使用的滚动视图是Custom,并且我在一个问题中描述了一个错误。也许你也可以帮忙?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 2012-03-31
    相关资源
    最近更新 更多