I have a ProgressBar custom view, and I would like the ability to set a custom fill on it. I can figure out how to do this with a color, but I want to be able to specify either a color or a gradient.
The problem is that ShapeStyle has an associated type, and there is no TypeErased version that I know of, so I have no way to store the fill.
Is there something I am missing, or a better way to do this?
struct ProgressBar<S:Shape> : View {
var percent:CGFloat
var shape:S
init(shape:S, percent:CGFloat) {
self.percent = percent
self.shape = shape
}
var body: some View {
ZStack(alignment: .leading) {
Rectangle().fill().relativeSize(width: percent, height: 1.0).clipShape(shape)
shape.stroke()
}
}
}
extension ProgressBar where S == RoundedRectangle {
init(corner:Length, percent:CGFloat) {
self.shape = RoundedRectangle(cornerRadius: corner)
self.percent = percent
}
}
extension ProgressBar where S == Rectangle {
init(percent: CGFloat) {
self.shape = Rectangle()
self.percent = percent
}
func cornerRadius(_ radius: Length) -> ProgressBar<RoundedRectangle> {
return ProgressBar<RoundedRectangle>(corner: radius, percent: self.percent)
}
}