Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created June 7, 2020 16:20
Show Gist options
  • Save prafullakumar/62adbea49f07c40faa3e97829a96a0b5 to your computer and use it in GitHub Desktop.
Save prafullakumar/62adbea49f07c40faa3e97829a96a0b5 to your computer and use it in GitHub Desktop.
struct GaugeView: View {
func colorMix(percent: Int) -> Color {
let p = Double(percent)
let tempG = (100.0-p)/100
let g: Double = tempG < 0 ? 0 : tempG
let tempR = 1+(p-100.0)/100.0
let r: Double = tempR < 0 ? 0 : tempR
return Color.init(red: r, green: g, blue: 0)
}
func tick(at tick: Int, totalTicks: Int) -> some View {
let percent = (tick * 100) / totalTicks
let startAngle = coveredRadius/2 * -1
let stepper = coveredRadius/Double(totalTicks)
let rotation = Angle.degrees(startAngle + stepper * Double(tick))
return VStack {
Rectangle()
.fill(colorMix(percent: percent))
.frame(width: tick % 2 == 0 ? 5 : 3,
height: tick % 2 == 0 ? 20 : 10) //alternet small big dash
Spacer()
}.rotationEffect(rotation)
}
let coveredRadius: Double // 0 - 360°
let maxValue: Int
let steperSplit: Int
private var tickCount: Int {
return maxValue/steperSplit
}
@Binding var value: Double
var body: some View {
ZStack {
ForEach(0..<tickCount*2 + 1) { tick in
self.tick(at: tick,
totalTicks: self.tickCount*2)
}
}.frame(width: 300, height: 300, alignment: .center)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment