You just don't need @State
wrapper for all those properties, so use just as
struct CircularProgressView: View {
@Binding var progress: CGFloat
private var lineWidth:CGFloat = 15
private var progressBarColor:Color = Color.red
private var tintColor:Color = Color.gray
// ... other code
func lineWidth(width:CGFloat) -> some View { // << here !!
var newView = self
newView.lineWidth = width
return newView
}
}
but @Binding
should remain as-is because it is like a reference to external source of truth (some dynamic property holding real value), binding itself does not do anything - it is just like a proxy.
So the following is useless
func progressBarColor(color:Binding<Color>) -> some View {
self.progressBarColor = color.wrappedValue
return self
}
you should use it like
struct ContentView: View {
@State private var progress = 0.3
var body: some View {
CircularProgressView(progress: $progress).lineWidth(width: 20)
// ... modify progress somewhere later
}
}
*** however I don't see in the provided scenario why would you need binding at all... if you'd remove it from CircularProgressView
, then the following would also work
struct ContentView: View {
@State private var progress = 0.3
var body: some View {
CircularProgressView(progress: progress).lineWidth(width: 20)
// ... modify progress somewhere later
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…