Latest 0.3.1
Homepage https://github.com/mac-gallagher/MGSegmentedProgressBar
License MIT
Platforms ios 9.0
Authors

Swift-Version
CocoaPods
license
CocoaPods

An animatable progress bar with support for multiple sections.

ProgressBar


Requirements

  • iOS 9.0+
  • Xcode 9.0+
  • Swift 4.1+

Installation

CocoaPods

MGSegmentedProgressBar is available through class ViewController: UIViewController { let progressBar = MGSegmentedProgressBar() let button1: UIButton = { let button = UIButton(type: .system) button.tag = 1 button.setTitle("Advance section 1", for: .normal) return button }() let button2: UIButton = { let button = UIButton(type: .system) button.tag = 2 button.setTitle("Advance section 1", for: .normal) return button }() override func viewDidLoad() { super.viewDidLoad() //configure progress bar view.addSubview(progressBar) progressBar.frame = CGRect(x: 50, y: 50, width: view.bounds.width - 100, height: 50) progressBar.dataSource = self //configure buttons view.addSubview(button1) button1.frame = CGRect(x: 50, y: 100, width: view.bounds.width - 100, height: 50) button1.addTarget(self, action: #selector(handleTouch), for: .touchUpInside) view.addSubview(button2) button2.frame = CGRect(x: 50, y: 150, width: view.bounds.width - 100, height: 50) button2.addTarget(self, action: #selector(handleTouch), for: .touchUpInside) } @objc func handleTouch(_ sender: UIButton) { progressBar.advance(section: sender.tag - 1) } } //MARK: Data Source extension ViewController: MGSegmentedProgressBarDataSource { func progressBar(_ progressBar: MGSegmentedProgressBar, barForSection section: Int) -> MGBarView { let bar = MGBarView() bar.backgroundColor = section % 2 == 0 ? .red : .blue return bar } func numberOfSections(in progressBar: MGSegmentedProgressBar) -> Int { return 2 } func numberOfSteps(in progressBar: MGSegmentedProgressBar) -> Int { return 10 } }

Architecture

There are two major components in the MGSegmentedProgressBar framework. The first is the MGSegmentedProgressBar which displays the individual bars. It is responsible for maintaining the overall progress and controlling the width of each bar. The second component is the bars themselves. Each MGBarView has an optional title which can be set via data source and delegate methods.

MGSegmentedProgressBar

To use a MGSegmentedProgressBar, add it to your view and implement the MGSegmentedProgressBarDataSource protocol. Each MGSegmentedProgressBar exposes the following variables:

var dataSource: MGSegmentedProgressBarDataSource?
var trackInset: CGFloat = 0
var trackBackgroundColor: UIColor?
var titleLabel: UILabel?
var labelEdgeInsets: UIEdgeInsets = .zero
var labelAlignment: MGLabelAlignment = .center
var lineCap: MGLineCap = .round

Updating your progress

There are two methods in MGSegmentedProgressBar you can call to update your bar’s progress. Both of these methods are animatable.

The first method resets the progress in the given section to the provided step count.

func setProgress(section: Int, steps: Int)

The second method advances the current section by the given number of steps.

func advance(section: Int, by numberOfSteps: Int = 1)

The method below resets your bar’s progress entirely. It is also animatable.

func resetProgress()

Data source & delegates

To load your bar views into your MGSegmentedProgressBar, you must conform your view controller to the MGSegmentedProgressBarDataSource protocol and implement the following functions:

func numberOfSteps(in progressBar: MGSegmentedProgressBar) -> Int
func numberOfSections(in progressBar: MGSegmentedProgressBar) -> Int
func progressBar(_ progressBar: MGSegmentedProgressBar, barForSection section: Int) -> MGBarView

The protocol also has a number of optional methods:

func progressBar(_ progressBar: MGSegmentedProgressBar, maximumNumberOfStepsForSection section: Int) -> Int
func progressBar(_ progressBar: MGSegmentedProgressBar, attributedTitleForSection section: Int) -> NSAttributedString?
func progressBar(_ progressBar: MGSegmentedProgressBar, titleForSection section: Int) -> String?

To customize your bar titles, conform your view controller to the MGSegmentedProgressBarDelegate protocol. All the methods in this protocol are optional.

func progressBar(_ progressBar: MGSegmentedProgressBar, titleInsetsForSection section: Int) -> UIEdgeInsets
func progressBar(_ progressBar: MGSegmentedProgressBar, titleAlignmentForSection section: Int) -> MGLabelAlignment
func progressBar(_ progressBar: MGSegmentedProgressBar, titleAlwaysVisibleForSection section: Int) -> Bool

MGBarView

The MGBarView is essentially a UIView with an optional title. Titles will only be visible if bar is large enough to fit the label. To change this behavior, set titleAlwaysVisible to true. EachMGBarView exposes the following variables:

var titleLabel: UILabel?
var labelEdgeInsets: UIEdgeInsets = .zero
var labelAlignment: MGLabelAlignment = .center
var titleAlwaysVisible: Bool = false

Author

Mac Gallagher, [email protected]

License

MGSegmentedProgressBar is available under the MIT License, see LICENSE for more infomation.

Latest podspec

{
    "name": "MGSegmentedProgressBar",
    "version": "0.3.1",
    "platforms": {
        "ios": "9.0"
    },
    "summary": "An animatable progress bar with support for multiple sections",
    "description": "An animatable progress bar with support for multiple sections.",
    "homepage": "https://github.com/mac-gallagher/MGSegmentedProgressBar",
    "screenshots": [
        "https://raw.githubusercontent.com/mac-gallagher/MGSegmentedProgressBar/master/Images/progress_bar.gif"
    ],
    "documentation_url": "https://github.com/mac-gallagher/MGSegmentedProgressBar/tree/master/README.md",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Mac Gallagher": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mac-gallagher/MGSegmentedProgressBar.git",
        "tag": "v0.3.1"
    },
    "swift_version": "4.1",
    "source_files": "MGSegmentedProgressBar/Classes/**/*"
}

Pin It on Pinterest

Share This