-
Notifications
You must be signed in to change notification settings - Fork 19
/
LinearProgressView.swift
124 lines (103 loc) · 3.69 KB
/
LinearProgressView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// LinearProgressView.swift
// LinearProgressView
//
// Created by Orkhan Alikhanov on 4/12/17.
// Copyright © 2017 BiAtoms. All rights reserved.
//
import UIKit
@IBDesignable
open class LinearProgressView: UIView {
private let trackView = UIView()
private lazy var trackViewWidthConstraint: NSLayoutConstraint = { NSLayoutConstraint(item: self.trackView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0) }()
@IBInspectable
open var barColor: UIColor = .gray {
didSet {
self.layer.backgroundColor = barColor.cgColor
}
}
@IBInspectable
open var trackColor: UIColor = .green {
didSet {
trackView.layer.backgroundColor = trackColor.cgColor
}
}
@IBInspectable
open var isCornersRounded: Bool = true {
didSet {
if !isCornersRounded {
self.layer.cornerRadius = 0
trackView.layer.cornerRadius = 0
}
layoutSubviews()
}
}
@IBInspectable
open var maximumValue: Float = 100.0 {
didSet {
progress = { progress }()
}
}
@IBInspectable
open var minimumValue: Float = 0.0 {
didSet {
progress = { progress }()
}
}
@IBInspectable
open private(set) var progress: Float = 0 {
didSet {
if progress > maximumValue {
progress = maximumValue
} else if progress < minimumValue {
progress = minimumValue
}
layoutSubviews()
}
}
@IBInspectable
open var barInset: CGFloat = 0 {
didSet {
layoutSubviews()
}
}
open var animationDuration: TimeInterval = 0.25
override init(frame: CGRect) {
super.init(frame: frame)
prepare()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
open func prepare() {
self.addSubview(trackView)
trackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
NSLayoutConstraint(item: trackView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .leftMargin, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: trackView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .topMargin, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: trackView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottomMargin, multiplier: 1.0, constant: 0),
trackViewWidthConstraint
])
self.layoutMargins = .zero
}
open func setProgress(_ value: Float, animated: Bool) {
self.progress = value
if animated {
UIView.animate(withDuration: animationDuration, animations: {
self.layoutIfNeeded()
})
}
}
override open func layoutSubviews() {
super.layoutSubviews()
let maxWidth: CGFloat = max(self.frame.width - barInset * 2, 0) // prevent from becoming negative
let calculatedWidth: CGFloat = maximumValue - minimumValue != 0 ? CGFloat((progress - minimumValue) / (maximumValue - minimumValue)) * maxWidth : 0
trackViewWidthConstraint.constant = calculatedWidth
if isCornersRounded {
self.layer.cornerRadius = self.frame.height / 2
trackView.layer.cornerRadius = trackView.frame.height / 2
}
self.layoutMargins = UIEdgeInsets(top: barInset, left: barInset, bottom: barInset, right: barInset)
}
}