gpt4 book ai didi

ios - 如何将渐变应用于任何 View 的 accentColor - SwiftUI

转载 作者:行者123 更新时间:2023-12-05 08:20:09 26 4
gpt4 key购买 nike

在这里,我正在使用 Slider。我们可以像这样改变 Slider 的 accentColor

Slider(value: $tip, in: 0...50, step: 1)
.accentColor(.red)

enter image description here

但是如何将 Gradient 应用于 accentColor 呢?

我尝试通过 mask slider 应用渐变,但后来我无法再滑动它了。

LinearGradient(gradient: Gradient(colors: [.red, .blue]), startPoint: .top, endPoint: .bottom)
.mask(Slider(value: $tip, in: 0...50, step: 1))

那么有什么间接的方法可以做到吗?

最佳答案

ZStack {
LinearGradient(
gradient: Gradient(colors: [.red, .blue]),
startPoint: .leading,
endPoint: .trailing
)
.mask(Slider(value: $val, in: 0...50, step: 1))

// Dummy replicated slider, to allow sliding
Slider(value: $val, in: 0...50, step: 1)
.opacity(0.05) // Opacity is the trick here.
// .accentColor(.clear)
// instead setting opacity,
// setting clear color is another alternative
// slider's circle remains white in this case
}

enter image description here

关于ios - 如何将渐变应用于任何 View 的 accentColor - SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com