From 450ece39c20fff0234ffcc937fcd6726b13445bb Mon Sep 17 00:00:00 2001 From: "Eliel A. Gordon" Date: Fri, 7 Feb 2020 13:12:03 -0800 Subject: [PATCH] add bar end cap style, update example --- .DS_Store | Bin 10244 -> 10244 bytes LinearProgressBar.xcodeproj/project.pbxproj | 41 ++++- .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../xcschemes/LinearProgressBar.xcscheme | 24 ++- .../LinearProgressBarExample.xcscheme | 12 +- .../xcschemes/LinearProgressBarTests.xcscheme | 6 +- LinearProgressBar/LinearProgressBar.swift | 24 ++- .../Base.lproj/Main.storyboard | 167 ++++++++++-------- LinearProgressBarExample/Info.plist | 2 + LinearProgressBarExample/ViewController.swift | 5 + 10 files changed, 169 insertions(+), 120 deletions(-) create mode 100644 LinearProgressBar.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/.DS_Store b/.DS_Store index bb2f3e1c4df20455831a3a32c1b37985af1773bc..9ec3b10312995c83a478a60b059bbd9e255767e2 100644 GIT binary patch delta 119 zcmZn(XbG6$&nUGqU^hRb)Z}@BYMYG&f|%HZgBTbXq$ewiC{JE1BrK7e@8Xh_lb-|> o + + + + IDEDidComputeMac32BitWarning + + + diff --git a/LinearProgressBar.xcodeproj/xcshareddata/xcschemes/LinearProgressBar.xcscheme b/LinearProgressBar.xcodeproj/xcshareddata/xcschemes/LinearProgressBar.xcscheme index fe76a3e..2ac2600 100644 --- a/LinearProgressBar.xcodeproj/xcshareddata/xcschemes/LinearProgressBar.xcscheme +++ b/LinearProgressBar.xcodeproj/xcshareddata/xcschemes/LinearProgressBar.xcscheme @@ -1,6 +1,6 @@ + + + + @@ -39,17 +48,6 @@ - - - - - - - - - - - - + + - - - - - - UIColor)? fileprivate var trackHeight: CGFloat { @@ -80,15 +86,18 @@ open class LinearProgressBar: UIView { context.setLineWidth(lineWidth) context.move(to: begin) context.addLine(to: end) - context.setLineCap(.round) + context.setLineCap(lineCap) context.strokePath() } func drawProgressView() { - guard let context = UIGraphicsGetCurrentContext() else {return} - - let beginPoint = CGPoint(x: barPadding + trackOffset, y: frame.size.height / 2) + guard let context = UIGraphicsGetCurrentContext() + else { return } + let beginPoint = CGPoint( + x: barPadding + trackOffset, + y: frame.size.height / 2 + ) // Progress Bar Track drawOn( @@ -96,19 +105,20 @@ open class LinearProgressBar: UIView { lineWidth: barThickness + trackPadding, begin: beginPoint, end: CGPoint(x: frame.size.width - barPadding - trackOffset, y: frame.size.height / 2), - lineCap: .round, + lineCap: CGLineCap(rawValue: capType) ?? .round, strokeColor: trackColor ) // Progress bar let colorForBar = barColorForValue?(Float(progressValue)) ?? barColor + let barLineWidth = calculatePercentage() > 0 ? barThickness : 0 drawOn( context: context, - lineWidth: barThickness, + lineWidth: barLineWidth, begin: beginPoint, end: CGPoint(x: barPadding + trackOffset + calculatePercentage(), y: frame.size.height / 2), - lineCap: .round, + lineCap: CGLineCap(rawValue: capType) ?? .round, strokeColor: colorForBar ) } diff --git a/LinearProgressBarExample/Base.lproj/Main.storyboard b/LinearProgressBarExample/Base.lproj/Main.storyboard index f76b452..59de3a3 100644 --- a/LinearProgressBarExample/Base.lproj/Main.storyboard +++ b/LinearProgressBarExample/Base.lproj/Main.storyboard @@ -1,12 +1,9 @@ - - - - + + - - + @@ -22,82 +19,100 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + - - - - - - + diff --git a/LinearProgressBarExample/Info.plist b/LinearProgressBarExample/Info.plist index 6f460e0..c1a3bbe 100644 --- a/LinearProgressBarExample/Info.plist +++ b/LinearProgressBarExample/Info.plist @@ -2,6 +2,8 @@ + UIUserInterfaceStyle + Light CFBundleDevelopmentRegion en CFBundleExecutable diff --git a/LinearProgressBarExample/ViewController.swift b/LinearProgressBarExample/ViewController.swift index d008916..a526207 100644 --- a/LinearProgressBarExample/ViewController.swift +++ b/LinearProgressBarExample/ViewController.swift @@ -11,6 +11,7 @@ import LinearProgressBar class ViewController: UIViewController { + @IBOutlet weak var endCapType: UISegmentedControl! @IBOutlet weak var linearProgressBar: LinearProgressBar! @IBOutlet weak var sliderView: UISlider! @@ -24,6 +25,10 @@ class ViewController: UIViewController { linearProgressBar.progressValue = CGFloat(sender.value) } + @IBAction func endCapTypeChanged(_ sender: UISegmentedControl) { + linearProgressBar.capType = Int32(sender.selectedSegmentIndex) + } + @IBAction func changeSwitchValue(_ sender: UISwitch) { if sender.isOn { // assign color closure