From aa71e0588ebc8336e8b5c360cddf2ab64afc9a62 Mon Sep 17 00:00:00 2001 From: James Ford Date: Fri, 29 Oct 2021 15:38:05 +0900 Subject: [PATCH 1/3] Edited to allow gauge face color and needle color changes --- Sources/TTGaugeView/TTGaugeView.swift | 24 +++++++++++++++---- Sources/TTGaugeView/TTGaugeViewSettings.swift | 22 +++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 Sources/TTGaugeView/TTGaugeViewSettings.swift diff --git a/Sources/TTGaugeView/TTGaugeView.swift b/Sources/TTGaugeView/TTGaugeView.swift index 30b9947..351e7b4 100644 --- a/Sources/TTGaugeView/TTGaugeView.swift +++ b/Sources/TTGaugeView/TTGaugeView.swift @@ -2,7 +2,7 @@ // TTGaugeView.swift // // Created by Tobias Tiemerding on 28.07.20. -// +// Modified by James Ford import SwiftUI @@ -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 @@ -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) } @@ -61,13 +68,17 @@ 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.settings = settings self.value = value self.valueDescription = valueDescription self.gaugeDescription = gaugeDescription @@ -78,6 +89,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}) { @@ -113,7 +129,7 @@ public struct TTGaugeView: View { } }, alignment: .bottom) - NeedleView(angle: angle, value: value) + NeedleView(angle: angle, value: value, needleColor: needleColor) } } } diff --git a/Sources/TTGaugeView/TTGaugeViewSettings.swift b/Sources/TTGaugeView/TTGaugeViewSettings.swift new file mode 100644 index 0000000..5ed58e3 --- /dev/null +++ b/Sources/TTGaugeView/TTGaugeViewSettings.swift @@ -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 + + } +} From 312101354780e01f12ac289d8bf113ce71fd0aa9 Mon Sep 17 00:00:00 2001 From: James Ford Date: Fri, 29 Oct 2021 15:43:48 +0900 Subject: [PATCH 2/3] Readme updated to reflect new feature implementation --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c6b6440..5f086d4 100644 --- a/README.md +++ b/README.md @@ -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 From dd5262cf5670a47b7f34e277037196039a94ea87 Mon Sep 17 00:00:00 2001 From: James Ford Date: Tue, 7 Dec 2021 13:24:38 +0900 Subject: [PATCH 3/3] added code to prevent gauge from going off scale high or low --- Sources/TTGaugeView/TTGaugeView.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/TTGaugeView/TTGaugeView.swift b/Sources/TTGaugeView/TTGaugeView.swift index 351e7b4..8799971 100644 --- a/Sources/TTGaugeView/TTGaugeView.swift +++ b/Sources/TTGaugeView/TTGaugeView.swift @@ -79,7 +79,16 @@ public struct TTGaugeView: View { self.angle = angle self.sections = sections self.settings = settings - self.value = value + //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 }