Advertisement
Guest User

Untitled

a guest
May 27th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //
  2. // DrawProgressBar.swift
  3. // HackatonAdidas
  4. //
  5. // Created by Charly Maxter on 27/5/18.
  6. // Copyright © 2018 HackatonAdidas. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class DrawProgressBar: NSObject {
  12. @objc dynamic public class func drawProgressBar(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 300, height: 14), resizing: ResizingBehavior = .aspectFit, progress: CGFloat = 0) {
  13. //// General Declarations
  14. let context = UIGraphicsGetCurrentContext()!
  15.  
  16. context.saveGState()
  17. let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 335, height: 14), target: targetFrame)
  18. context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY)
  19. context.scaleBy(x: resizedFrame.width / 300, y: resizedFrame.height / 14)
  20.  
  21. //// Color Declarations
  22. let color8 = UIColor(red: 0.129, green: 0.671, blue: 0.647, alpha: 1.000)
  23. let color5 = UIColor(red: 0.200, green: 0.827, blue: 0.537, alpha: 1.000)
  24. let color = UIColor(red: 0.086, green: 0.227, blue: 0.373, alpha: 1.000)
  25.  
  26.  
  27.  
  28. //// Progress border Drawing
  29. let progressBorderPath = UIBezierPath(roundedRect: CGRect(x: 2, y: 0, width: 297, height: 14), cornerRadius: 7)
  30. color.setFill()
  31. progressBorderPath.fill()
  32. color8.setStroke()
  33. progressBorderPath.lineWidth = 2
  34. progressBorderPath.lineCapStyle = .round
  35. progressBorderPath.stroke()
  36.  
  37.  
  38. //// Progress Active Drawing
  39. let progressActivePath = UIBezierPath(roundedRect: CGRect(x: 2, y: 0, width: progress, height: 14), cornerRadius: 7)
  40. color5.setFill()
  41. progressActivePath.fill()
  42. color8.setStroke()
  43. progressActivePath.lineWidth = 2
  44. progressActivePath.stroke()
  45.  
  46. }
  47.  
  48. @objc(StyleKitNameResizingBehavior)
  49. public enum ResizingBehavior: Int {
  50. case aspectFit /// The content is proportionally resized to fit into the target rectangle.
  51. case aspectFill /// The content is proportionally resized to completely fill the target rectangle.
  52. case stretch /// The content is stretched to match the entire target rectangle.
  53. case center /// The content is centered in the target rectangle, but it is NOT resized.
  54.  
  55. public func apply(rect: CGRect, target: CGRect) -> CGRect {
  56. if rect == target || target == CGRect.zero {
  57. return rect
  58. }
  59.  
  60. var scales = CGSize.zero
  61. scales.width = abs(target.width / rect.width)
  62. scales.height = abs(target.height / rect.height)
  63.  
  64. switch self {
  65. case .aspectFit:
  66. scales.width = min(scales.width, scales.height)
  67. scales.height = scales.width
  68. case .aspectFill:
  69. scales.width = max(scales.width, scales.height)
  70. scales.height = scales.width
  71. case .stretch:
  72. break
  73. case .center:
  74. scales.width = 1
  75. scales.height = 1
  76. }
  77.  
  78. var result = rect.standardized
  79. result.size.width *= scales.width
  80. result.size.height *= scales.height
  81. result.origin.x = target.minX + (target.width - result.width) / 2
  82. result.origin.y = target.minY + (target.height - result.height) / 2
  83. return result
  84. }
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement