Created
April 27, 2018 01:45
-
-
Save DanielCardonaRojas/ceebc043d476f689c14fa05784b8d9d3 to your computer and use it in GitHub Desktop.
Progress View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProgressView.swift | |
// ProgressView | |
// | |
// Created by Daniel Cardona on 4/26/18. | |
// Copyright © 2018 Daniel Cardona. All rights reserved. | |
// | |
import UIKit | |
class ProgressView: UIView { | |
var progress : Float = 0 { | |
didSet { | |
displayProgress(progress) | |
} | |
} | |
private var progressView : UIView! | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setupView() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.setupView() | |
} | |
private func displayProgress(_ progress: Float) { | |
let p = CGFloat(ProgressView.constrained(progress, max: 1, min: 0)) | |
let transform = CGAffineTransform(scaleX: p, y: 1.0) | |
let newSize = self.frame.size.applying(transform) | |
let offset = frame.size.width - newSize.width | |
let newOrigin = CGPoint(x: offset , y: 0) | |
progressView.frame = CGRect(origin: newOrigin, size: newSize) | |
refreshViews() | |
} | |
func setupView() { | |
progressView = UIView(frame: self.bounds) | |
self.addSubview(progressView) | |
progressView.backgroundColor = .green | |
displayProgress(progress) | |
} | |
func refreshViews() { | |
setNeedsDisplay() | |
progressView.setNeedsDisplay() | |
} | |
// MARK: Helpers | |
private static func constrained(_ scalar: Float, max: Float, min: Float) -> Float { | |
if scalar > max { | |
return max | |
} | |
if scalar < min { | |
return min | |
} | |
return scalar | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment