Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created June 7, 2020 16:26
Show Gist options
  • Save prafullakumar/d612f84cc446844d991e8762e49d638d to your computer and use it in GitHub Desktop.
Save prafullakumar/d612f84cc446844d991e8762e49d638d to your computer and use it in GitHub Desktop.
struct Needle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: 0, y: rect.height/2))
path.addLine(to: CGPoint(x: rect.width, y: 0))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
return path
}
}
struct GaugeView: View {
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 {
Text("\(value, specifier: "%0.0f")")
.font(.system(size: 40, weight: Font.Weight.bold))
.foregroundColor(Color.orange)
.offset(x: 0, y: 40)
Needle()
.fill(Color.red)
.frame(width: 140, height: 6)
.offset(x: -70, y: 0)
.rotationEffect(.init(degrees: getAngle(value: value)), anchor: .center)
.animation(.linear)
Circle()
.frame(width: 20, height: 20)
.foregroundColor(.red)
}.frame(width: 300, height: 300, alignment: .center)
}
func getAngle(value: Double) -> Double {
return (value/Double(maxValue))*coveredRadius - coveredRadius/2 + 90
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment