Skip to content

Commit 3b428ff

Browse files
improve stripes display in circular progress
1 parent 1b20172 commit 3b428ff

File tree

3 files changed

+31
-59
lines changed

3 files changed

+31
-59
lines changed

Sources/ComponentsKit/Components/CircularProgress/Models/CircularProgressVM.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ extension CircularProgressVM {
8181
return .lgCaption
8282
}
8383
}
84+
var stripeWidth: CGFloat {
85+
return 0.5
86+
}
8487
private func stripesCGPath(in rect: CGRect) -> CGMutablePath {
85-
let stripeWidth: CGFloat = 0.5
8688
let stripeSpacing: CGFloat = 3
8789
let stripeAngle: Angle = .degrees(135)
8890

@@ -93,14 +95,10 @@ extension CircularProgressVM {
9395
let dx: CGFloat = rect.height * tan(radians)
9496
for x in stride(from: 0, through: rect.width + rect.height, by: step) {
9597
let topLeft = CGPoint(x: x, y: 0)
96-
let topRight = CGPoint(x: x + stripeWidth, y: 0)
97-
let bottomLeft = CGPoint(x: x + dx, y: rect.height)
98-
let bottomRight = CGPoint(x: x + stripeWidth + dx, y: rect.height)
98+
let bottomRight = CGPoint(x: x + dx, y: rect.height)
9999

100100
path.move(to: topLeft)
101-
path.addLine(to: topRight)
102101
path.addLine(to: bottomRight)
103-
path.addLine(to: bottomLeft)
104102
path.closeSubpath()
105103
}
106104
return path
@@ -109,15 +107,7 @@ extension CircularProgressVM {
109107

110108
extension CircularProgressVM {
111109
func gap(for normalized: CGFloat) -> CGFloat {
112-
normalized > 0 ? 0.05 : 0
113-
}
114-
115-
func progressArcStart(for normalized: CGFloat) -> CGFloat {
116-
return 0
117-
}
118-
119-
func progressArcEnd(for normalized: CGFloat) -> CGFloat {
120-
return max(0, min(1, normalized))
110+
return normalized > 0 ? 0.05 : 0
121111
}
122112

123113
func stripedArcStart(for normalized: CGFloat) -> CGFloat {

Sources/ComponentsKit/Components/CircularProgress/SUCircularProgress.swift

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -102,52 +102,33 @@ public struct SUCircularProgress: View {
102102
}
103103

104104
var stripedBackground: some View {
105-
Path { path in
106-
path.addArc(
107-
center: self.model.center,
108-
radius: self.model.radius,
109-
startAngle: .radians(0),
110-
endAngle: .radians(2 * .pi),
111-
clockwise: false
112-
)
113-
}
114-
.trim(
115-
from: self.model.stripedArcStart(for: self.progress),
116-
to: self.model.stripedArcEnd(for: self.progress)
117-
)
118-
.stroke(
119-
.clear,
120-
style: StrokeStyle(
121-
lineWidth: self.model.circularLineWidth,
122-
lineCap: .round
105+
StripesShapeCircularProgress(model: self.model)
106+
.stroke(
107+
self.model.color.main.color,
108+
style: StrokeStyle(lineWidth: self.model.stripeWidth)
123109
)
124-
)
125-
.overlay {
126-
StripesShapeCircularProgress(model: self.model)
127-
.foregroundColor(self.model.color.main.color)
128-
.mask {
129-
Path { maskPath in
130-
maskPath.addArc(
131-
center: self.model.center,
132-
radius: self.model.radius,
133-
startAngle: .radians(0),
134-
endAngle: .radians(2 * .pi),
135-
clockwise: false
136-
)
137-
}
138-
.trim(
139-
from: self.model.stripedArcStart(for: self.progress),
140-
to: self.model.stripedArcEnd(for: self.progress)
141-
)
142-
.stroke(
143-
style: StrokeStyle(
144-
lineWidth: self.model.circularLineWidth,
145-
lineCap: .round
146-
)
110+
.mask {
111+
Path { maskPath in
112+
maskPath.addArc(
113+
center: self.model.center,
114+
radius: self.model.radius,
115+
startAngle: .radians(0),
116+
endAngle: .radians(2 * .pi),
117+
clockwise: false
147118
)
148119
}
149-
}
150-
.rotationEffect(.degrees(-90))
120+
.trim(
121+
from: self.model.stripedArcStart(for: self.progress),
122+
to: self.model.stripedArcEnd(for: self.progress)
123+
)
124+
.stroke(
125+
style: StrokeStyle(
126+
lineWidth: self.model.circularLineWidth,
127+
lineCap: .round
128+
)
129+
)
130+
}
131+
.rotationEffect(.degrees(-90))
151132
}
152133
}
153134

Sources/ComponentsKit/Components/CircularProgress/UKCircularProgress.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ extension UKCircularProgress {
237237
model: CircularProgressVM
238238
) {
239239
layer.isHidden = model.isStripesLayerHidden
240-
layer.fillColor = model.color.main.uiColor.cgColor
240+
layer.strokeColor = model.color.main.uiColor.cgColor
241+
layer.lineWidth = model.stripeWidth
241242
}
242243

243244
static func stripesMaskLayer(

0 commit comments

Comments
 (0)