【发布时间】:2021-02-04 19:42:39
【问题描述】:
我有这个用 SwiftUI 编写的 CircularProgressView。我想创建一种方法来添加一些自定义修饰符,这样我就可以从父视图中更改一些属性。下面的代码不起作用。
我说的是 func lineWidth(width:CGFloat) -> some View , func progressBarColor(color:Binding) -> some View { , func tintColor(color:Color) -> 一些视图 {
struct CircularProgressView: View {
@Binding var progress: CGFloat
@State private var lineWidth:CGFloat = 15
@State private var progressBarColor:Color = Color.red
@State private var tintColor:Color = Color.gray
func lineWidth(width:CGFloat) -> some View {
self.lineWidth = width
return self
}
func progressBarColor(color:Color) -> some View {
self.progressBarColor = color
return self
}
func tintColor(color:Color) -> some View {
self.tintColor = color
return self
}
var body: some View {
GeometryReader { geometry in
ZStack {
Circle()
.stroke(tintColor, lineWidth: lineWidth)
.aspectRatio(1, contentMode: .fit)
Circle()
.trim(from: 0.0, to: progress)
.stroke(progressBarColor, lineWidth: lineWidth)
.rotationEffect(Angle(degrees: -90))
.aspectRatio(1, contentMode: .fit)
}
}
}
}
例如。我想像这样使用这个 CircularProgressView:
struct ContentView: View {
var body: some View {
CircularProgressView(progress: .constant(0.3)).lineWidth(width: 20)
}
}
如何使用 ViewModifiers 实现这一点?我认为这一切都错了吗?
【问题讨论】:
-
什么不行,你想达到什么,不清楚?
-
我做了一些改变。对此感到抱歉