Created
June 7, 2020 16:23
-
-
Save prafullakumar/6c1abf185adcf7243e025b1ca1cdd4d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
func tickText(at tick: Int, text: String) -> some View { | |
let percent = (tick * 100) / tickCount | |
let startAngle = coveredRadius/2 * -1 + 90 | |
let stepper = coveredRadius/Double(tickCount) | |
let rotation = startAngle + stepper * Double(tick) | |
return Text(text).foregroundColor(colorMix(percent: percent)).rotationEffect(.init(degrees: -1 * rotation), anchor: .center).offset(x: -115, y: 0).rotationEffect(Angle.degrees(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) | |
} | |
ForEach(0..<tickCount+1) { tick in | |
self.tickText(at: tick, text: "\(self.steperSplit*tick)") | |
} | |
}.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