MyProgressBar2 ๐ฆ
ๆ่ชๅทฑ่จญ่จ็้ฒๅบฆๆข (ไธๅ ๅซๆจ้ก็้จๅ)๏ผๅฏ่จญๅฎๅ ง่ท (padding)ใๅค่ท (margin) ่้ฒๅบฆๆข้ซๅบฆใ
Last updated
ๆ่ชๅทฑ่จญ่จ็้ฒๅบฆๆข (ไธๅ ๅซๆจ้ก็้จๅ)๏ผๅฏ่จญๅฎๅ ง่ท (padding)ใๅค่ท (margin) ่้ฒๅบฆๆข้ซๅบฆใ
Last updated
import SwiftUI
/// โญ๏ธ `NeuProgressBar2` ็ใ้ฒๅบฆๆขใ้จๅ
public struct MyProgressBar2<Bar: View>: View {
let height : CGFloat // ้ฒๅบฆๆข้ซๅบฆ
let padding: CGFloat // ้ฒๅบฆๆข่ๅนๆงฝ้็ทฃ็้่ท
let margin : CGFloat // ๅนๆงฝไธไธ้็ทฃ่ๅค็็้่ท
let bar : Bar // ้ฒๅบฆๆข็ๆจฃๅผ
@Binding var percent: CGFloat // 0.0 ~ 1.0
public init(
_ percent: Binding<CGFloat>,
height : CGFloat = 10,
padding : CGFloat = 2,
margin : CGFloat = 0,
@ViewBuilder bar: () -> Bar // โญ๏ธ @ViewBuilder closure
) {
self._percent = percent // โญ๏ธ ๅณ้ฒ @Binding ่ฎๆธ
self.height = height
self.padding = padding
self.margin = margin
self.bar = bar()
}
public var body: some View {
let h = height
let p = padding
let m = margin
// โญ๏ธ ็บไบ่จ็ฎ้ฒๅบฆๆขใ่่ฒ้จๅใ็ๅฏฌๅบฆ๏ผไฝฟ็จ `GeometryReader`
return GeometryReader { geometry in
ZStack(alignment: .leading) {
// ๅนๆงฝ
Capsule()
.frame(height: h + 2 * p) // โญ๏ธ ๅฎ็พฉๅนๆงฝ็ใ้ซๅบฆใ
.foregroundColor(Neu.color.cardBackground)
// ๅนๆงฝๆผธๅฑคๆๆ (groove gradient)
.overlay(Neu.progressBarGroove.opacity(0.7)) // โญ๏ธ ่ฎ่ๆฏๅ้้ไพไธไบ
.clipShape(Capsule())
// ้ฒๅบฆๆข
self.bar // โญ๏ธ ้ฒๅบฆๆขใๆจฃๅผใ
.frame(height: h) // โญ๏ธ ้ฒๅบฆๆขใ้ซๅบฆใ
.mask( // โญ๏ธ ๅฐ้ฒๅบฆๆขไพใไธ้ข็ๅฝข็ใๅชไธไพ
Capsule() // โญ๏ธ ้ฒๅบฆๆขใๅฝข็ใ
// โญ๏ธ ่จ็ฎ้ฒๅบฆๆขใๅพ้ข็ฉบ็ฝ้จๅ็ๅฏฌๅบฆใ
.padding(.trailing, (geometry.size.width - 2 * p) * (1 - self.percent))
// โญ๏ธ ้ฒๅบฆๆขๅๅพๅๅๆฃๆไธๅ padding
.padding(.horizontal, p)
) // mask
.shadow(color: Neu.color.text.opacity(0.7), radius: 2, x: 0, y: 1)
} // ZStack
}// container (GeometryReader)
.frame(height: h + 2 * p + 2 * m) // โญ๏ธ ่จ็ฎใ็ธฝ้ซๅบฆใ
}
}
// โญ๏ธ ็ตฆ้ฒๅบฆๆขใ้ ่จญๆจฃๅผใ
extension MyProgressBar2 where Bar == LinearGradient {
public init(
_ percent: Binding<CGFloat>,
height : CGFloat = 10,
padding : CGFloat = 2,
margin : CGFloat = 0
) {
self._percent = percent // โญ๏ธ ๅณ้ฒ @Binding ่ฎๆธ็ๆนๅผ
self.height = height
self.padding = padding
self.margin = margin
// โญ๏ธ ็ตฆ้ฒๅบฆๆขใ้ ่จญๆจฃๅผใ
self.bar = Gradient.right(Color.yellow.opacity(0.8), .red) as! Bar
}
}