update multiprogressbar
This commit is contained in:
parent
595a3b1e38
commit
d52e204721
@ -10,57 +10,78 @@ import UIKit
|
|||||||
|
|
||||||
@objcMembers open class MultiProgress: MFView {
|
@objcMembers open class MultiProgress: MFView {
|
||||||
|
|
||||||
var progressList = Array<[AnyHashable: Any]>()
|
var progressList: Array<ProgressBarObject>?
|
||||||
var roundedRect : Bool = false
|
var roundedRect: Bool = false {
|
||||||
var thicknessConstraint : NSLayoutConstraint?
|
didSet {
|
||||||
|
if roundedRect {
|
||||||
let defaultHeight : CGFloat = 8
|
layer.cornerRadius = (thicknessConstraint?.constant ?? defaultHeight)/2
|
||||||
|
} else {
|
||||||
|
layer.cornerRadius = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var thicknessConstraint: NSLayoutConstraint?
|
||||||
|
let defaultHeight: CGFloat = 8
|
||||||
|
|
||||||
override open func setupView() {
|
override open func setupView() {
|
||||||
super.setupView()
|
super.setupView()
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
translatesAutoresizingMaskIntoConstraints = false
|
||||||
backgroundColor = .mfSilver()
|
backgroundColor = .mfSilver()
|
||||||
clipsToBounds = true
|
clipsToBounds = true
|
||||||
|
if thicknessConstraint == nil {
|
||||||
|
thicknessConstraint = heightAnchor.constraint(equalToConstant: defaultHeight)
|
||||||
|
thicknessConstraint?.isActive = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override open func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
override open func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
|
thicknessConstraint?.constant = json?.optionalCGFloatForKey("thickness") ?? defaultHeight
|
||||||
thicknessConstraint?.isActive = false
|
roundedRect = json?.optionalBoolForKey("roundedRect") ?? false
|
||||||
thicknessConstraint = nil
|
guard let list = json?.optionalArrayForKey("progressList") as? [[AnyHashable: Any]], let progressObjectList = ProgressBarObject.getProgressBarObjectList(list), progressObjectList.count > 0 else {
|
||||||
let height = json?.optionalCGFloatForKey("thickness") ?? defaultHeight
|
return
|
||||||
let constraint = heightAnchor.constraint(equalToConstant: height)
|
|
||||||
constraint.isActive = true
|
|
||||||
thicknessConstraint = constraint
|
|
||||||
|
|
||||||
self.roundedRect = json?.optionalBoolForKey("roundedRect") ?? false
|
|
||||||
if self.roundedRect {
|
|
||||||
self.layer.cornerRadius = (thicknessConstraint?.constant ?? defaultHeight)/2
|
|
||||||
}
|
}
|
||||||
if let list = json?.optionalArrayForKey("progressList") as? [[AnyHashable: Any]] {
|
progressList = progressObjectList
|
||||||
progressList = list
|
var previous: UIView?
|
||||||
var previous : UIView?
|
for progressObject in progressList! {
|
||||||
for module in progressList {
|
guard progressObject.progress > 0.0 else {
|
||||||
let view = MFView(frame: .zero)
|
continue
|
||||||
view.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
addSubview(view)
|
|
||||||
view.backgroundColor = UIColor.mfGet(forHex: module.optionalStringForKey("progressColor") ?? "#007bb8")
|
|
||||||
view.widthAnchor.constraint(equalTo: widthAnchor, multiplier: (module.optionalCGFloatForKey("progress") ?? 0)/100).isActive = true
|
|
||||||
if previous != nil {
|
|
||||||
view.leadingAnchor.constraint(equalTo: previous!.trailingAnchor).isActive = true
|
|
||||||
previous = view
|
|
||||||
} else {
|
|
||||||
let constraint = view.leadingAnchor.constraint(equalTo: leadingAnchor)
|
|
||||||
constraint.priority = UILayoutPriority(rawValue: 900)
|
|
||||||
constraint.isActive = true
|
|
||||||
previous = view
|
|
||||||
}
|
|
||||||
NSLayoutConstraint.constraintPinSubview(view, pinTop: true, pinBottom: true, pinLeft: false, pinRight: false)
|
|
||||||
}
|
}
|
||||||
guard previous != nil else {return}
|
let view = MFView(frame: .zero)
|
||||||
let constraint = trailingAnchor.constraint(equalTo: previous!.trailingAnchor)
|
view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
constraint.priority = UILayoutPriority(800)
|
addSubview(view)
|
||||||
constraint.isActive = true
|
view.backgroundColor = progressObject.color
|
||||||
|
view.widthAnchor.constraint(equalTo: widthAnchor, multiplier: progressObject.progress).isActive = true
|
||||||
|
view.leadingAnchor.constraint(equalTo: previous?.trailingAnchor ?? leadingAnchor).isActive = true
|
||||||
|
previous = view
|
||||||
|
NSLayoutConstraint.constraintPinSubview(view, pinTop: true, pinBottom: true, pinLeft: false, pinRight: false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@objcMembers open class ProgressBarObject {
|
||||||
|
///form 0.0 to 1.0. input progress should be [0 100]
|
||||||
|
var progress: CGFloat = 0.0
|
||||||
|
///default color is cerulean
|
||||||
|
var color: UIColor = UIColor.mfCerulean()
|
||||||
|
|
||||||
|
init(_ module: [AnyHashable: Any]?) {
|
||||||
|
progress = (module?.optionalCGFloatForKey("progress") ?? 0.0)/100
|
||||||
|
if let colorString = module?.optionalStringForKey("progressColor") {
|
||||||
|
color = UIColor.mfGet(forHex: colorString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func getProgressBarObjectList(_ list: [[AnyHashable: Any]]?) -> [ProgressBarObject]? {
|
||||||
|
guard list?.count ?? 0 > 0 else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var progressList = [ProgressBarObject]()
|
||||||
|
for module in list! {
|
||||||
|
let progressObject = ProgressBarObject(module)
|
||||||
|
progressList.append(progressObject)
|
||||||
|
}
|
||||||
|
return progressList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user