Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ let sections: [TTGaugeViewSection] = [TTGaugeViewSection(color: .green, size: 0.
TTGaugeViewSection(color: .red, size: 0.1),
TTGaugeViewSection(color: .purple, size: 0.2),
TTGaugeViewSection(color: .blue, size: 0.4)]


let settings: TTGaugeViewSettings = TTGaugeViewSettings(faceColor: .blue, needleColor: .orange)
var value: Double
let valueDescription = "\(Int(value * 100)) %"
let gaugeDescription = "My SwiftUI Gauge"

TTGaugeView(angle: angle, sections: sections, value: value, valueDescription: valueDescription, gaugeDescription: gaugeDescription)
TTGaugeView(angle: angle, sections: sections, settings: settings, value: value, valueDescription: valueDescription, gaugeDescription: gaugeDescription)
```

## Contributing to this project
Expand Down
35 changes: 30 additions & 5 deletions Sources/TTGaugeView/TTGaugeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// TTGaugeView.swift
//
// Created by Tobias Tiemerding on 28.07.20.
//
// Modified by James Ford

import SwiftUI

Expand Down Expand Up @@ -30,6 +30,7 @@ struct GaugeElement: View {
struct NeedleView: View {
var angle: Double
var value: Double = 0.0
var needleColor: Color?

var body: some View {
// 90 to start in south orientation, then add offset to keep gauge symetric
Expand All @@ -43,13 +44,19 @@ struct NeedleView: View {
let rectHeight = geometry.size.width / 20

Rectangle()
.fill(Color.black.opacity(0.8))
.fill( //default color is black
{ needleColor ?? Color.black }()
.opacity(0.8)
)
.cornerRadius(rectWidth / 2)
.frame(width: rectWidth, height: rectHeight)
.offset(x: rectWidth / 2)

Circle()
.frame(width: geometry.size.width / 10)
.foregroundColor(
{ needleColor ?? Color.black }()
)
}
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
}
Expand All @@ -61,14 +68,27 @@ struct NeedleView: View {
public struct TTGaugeView: View {
var angle: Double
var sections: [TTGaugeViewSection]
var settings: TTGaugeViewSettings?
var value: Double
var valueDescription: String?
var gaugeDescription: String?
var backgroundColor: Color? { return settings?.faceColor } // both color variables were here already
var needleColor: Color? { return settings?.needleColor } // but not implemented

public init(angle: Double, sections: [TTGaugeViewSection], value: Double, valueDescription: String? = nil, gaugeDescription: String? = nil) {
public init(angle: Double, sections: [TTGaugeViewSection], settings: TTGaugeViewSettings? = nil, value: Double, valueDescription: String? = nil, gaugeDescription: String? = nil) {
self.angle = angle
self.sections = sections
self.value = value
self.settings = settings
//keeps gauge from going off scale
self.value = {
if value < 0 {
return 0
}
else if value > 1 {
return 1
}
return value
}()
self.valueDescription = valueDescription
self.gaugeDescription = gaugeDescription
}
Expand All @@ -78,6 +98,11 @@ public struct TTGaugeView: View {
let startAngle = 90 + (360.0-angle) / 2.0

ZStack {

if (backgroundColor != nil) { //if no color is given, it defaults to clear
Circle()
.foregroundColor(backgroundColor)
}
ForEach(sections) { section in
// Find index of current section to sum up already covered areas in percent
if let index = sections.firstIndex(where: {$0.id == section.id}) {
Expand Down Expand Up @@ -113,7 +138,7 @@ public struct TTGaugeView: View {
}
}, alignment: .bottom)

NeedleView(angle: angle, value: value)
NeedleView(angle: angle, value: value, needleColor: needleColor)
}
}
}
22 changes: 22 additions & 0 deletions Sources/TTGaugeView/TTGaugeViewSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// TTGaugeViewSettings.swift
//
//
// Created by James Ford
// Modified to allow further customization of Tobias Tiemerding's TTGaugeView
//

import Foundation
import SwiftUI

public struct TTGaugeViewSettings: Identifiable {
public var id = UUID()
var faceColor: Color?
var needleColor: Color?

public init(faceColor: Color? = nil, needleColor: Color? = nil) {
self.faceColor = faceColor
self.needleColor = needleColor

}
}