ProgressView.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // ProgressView.swift
  3. // DashKitUI
  4. //
  5. // Created by Pete Schwamb on 3/2/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public struct ProgressView: View {
  10. private let progress: CGFloat
  11. private let barHeight: CGFloat = 6
  12. public init(progress: CGFloat) {
  13. self.progress = progress
  14. }
  15. public var body: some View {
  16. return GeometryReader { geometry in
  17. ZStack(alignment: .leading) {
  18. Rectangle()
  19. .opacity(0.1)
  20. .cornerRadius(self.barHeight/2)
  21. .animation(nil)
  22. Rectangle()
  23. .foregroundColor(Color.accentColor)
  24. .frame(width: geometry.size.width * self.progress)
  25. .cornerRadius(self.barHeight/2)
  26. }
  27. }
  28. .frame(height: barHeight)
  29. }
  30. }
  31. struct ProgressTestView: View {
  32. @State var showDetail: Bool = false
  33. @State var madeProgress: Bool = false
  34. var body: some View {
  35. VStack {
  36. ProgressView(progress: madeProgress ? 0.9 : 0.5)
  37. .animation(.linear(duration: 2))
  38. .padding()
  39. .opacity(showDetail ? 1 : 0)
  40. .animation(.linear(duration: 0.2))
  41. Button("Test") {
  42. self.showDetail.toggle()
  43. self.madeProgress.toggle()
  44. }
  45. }
  46. .animation(.linear(duration: 0.2))
  47. }
  48. }
  49. struct ProgressView_Previews: PreviewProvider {
  50. @State var showDetail: Bool = false
  51. static var previews: some View {
  52. ProgressTestView()
  53. }
  54. }