是否可以将 "Weights" 添加到 SwiftUI 渐变中的颜色?

Is it possible to add "Weights" to the colors in a SwiftUI Gradient?

我正在使用颜色渐变作为我的应用程序的背景(在 SwiftUI 中)。

为此,我刚刚将背景设置为线性颜色渐变,如下所示(缩写):

private var grade = Gradient(colors: [Color("Red"), Color("Blue")])

...

.background(LinearGradient(gradient: grade, startPoint: .top, endPoint: .bottom))

我希望渐变在用户增加圆圈中的数字(通过拖动小圆圈)时为 'red-shifted',而在较小的数字时为 'blue-shifted'。

我尝试过的一件事是将颜色设置为一个非常大的数组,从一半开始是 Color("Red"),一半是 Color("Blue");然后我想我可以在用户拖动时更改红色的数量;但是,这也会减少应用的渐变量(它看起来更像是一个红色框和一个蓝色框,然后是渐变)。它确实适用于较小的规模,所以我将颜色设置为 [Color("Red"), Color("Red"), Color("Blue")] 以显示我希望它在更大的范围内看起来像什么人数:

虽然我希望这种效果是连续的(或有效连续的)。

我希望这展示了我正在努力实现的目标,并且一如既往地感谢您的帮助!

这是可能的解决方案。使用 Xcode 11.4 / iOS 13.4

测试

注:当然可以根据需要修改参数

struct DemoGradientShift: View {
    private var grade = Gradient(colors: [.red, .blue])
    @State private var value: CGFloat = 50
    var body: some View {
        ZStack {
            VStack {
                Rectangle().fill(Color.red)
                Rectangle().fill(Color.blue)
            }
            Rectangle().fill(LinearGradient(gradient: grade, startPoint: .top, endPoint: .bottom))
                .offset(x: 0, y: 5 * (50 - value))
            Slider(value: $value, in: 1...100)
        }
        .edgesIgnoringSafeArea(.all)
    }
}