2019-10-20 19:08:12 +00:00
|
|
|
//
|
|
|
|
// ProgressView.swift
|
2021-09-21 03:10:24 +00:00
|
|
|
// Twigs
|
2019-10-20 19:08:12 +00:00
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 10/20/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ProgressView: View {
|
2021-09-21 01:59:13 +00:00
|
|
|
var value: Float
|
|
|
|
var maxValue: Float = 100.0
|
2019-10-20 19:08:12 +00:00
|
|
|
var progressTintColor: Color = .blue
|
2021-09-21 01:59:13 +00:00
|
|
|
var progressBarHeight: Float = 20
|
|
|
|
var progressBarCornerRadius: Float = 4.0
|
2022-05-19 02:05:03 +00:00
|
|
|
@State
|
|
|
|
var progressBarWidth: CGFloat = 0.0
|
2019-10-20 19:08:12 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
GeometryReader { geometry in
|
|
|
|
ZStack(alignment: .leading) {
|
|
|
|
Rectangle()
|
|
|
|
.opacity(0.1)
|
|
|
|
Rectangle()
|
2021-09-21 01:59:13 +00:00
|
|
|
.frame(width: getProgressBarWidth(geometry: geometry))
|
2019-10-20 19:08:12 +00:00
|
|
|
.opacity(0.5)
|
|
|
|
.background(self.progressTintColor)
|
2022-05-19 02:05:03 +00:00
|
|
|
.animation(.default, value: progressBarWidth)
|
|
|
|
.onAppear {
|
|
|
|
self.progressBarWidth = getProgressBarWidth(geometry: geometry)
|
|
|
|
}
|
2021-09-21 01:59:13 +00:00
|
|
|
}.frame(height: CGFloat(self.progressBarHeight))
|
|
|
|
.cornerRadius(CGFloat(self.progressBarCornerRadius))
|
2019-10-20 19:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getProgressBarWidth(geometry: GeometryProxy) -> CGFloat {
|
|
|
|
let frame = geometry.frame(in: .global)
|
2022-04-16 02:16:33 +00:00
|
|
|
return max(0, frame.size.width * min(CGFloat(value / maxValue), CGFloat(1)))
|
2019-10-20 19:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ProgressView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-09-21 01:59:13 +00:00
|
|
|
VStack {
|
|
|
|
Text("0%")
|
|
|
|
ProgressView(value: 1.0, maxValue: 0.0, progressTintColor: .red)
|
|
|
|
Text("25%")
|
|
|
|
ProgressView(value: 1.0, maxValue: 4.0, progressTintColor: .green)
|
|
|
|
Text("50%")
|
|
|
|
ProgressView(value: 1.0, maxValue: 2.0, progressTintColor: .blue)
|
|
|
|
Text("66%")
|
|
|
|
ProgressView(value: 2.0, maxValue: 3.0, progressTintColor: .orange)
|
|
|
|
Text("150%")
|
|
|
|
ProgressView(value: 150.0, maxValue: 100.0, progressTintColor: .purple)
|
|
|
|
}.padding(50)
|
2019-10-20 19:08:12 +00:00
|
|
|
}
|
|
|
|
}
|