// // ResourcesViewChart.swift // Wallet // // Created by Igor on 09.03.2021. // Copyright © 2021 AM. All rights reserved. // import UIKit class ResourcesViewChartDraw: UIView { var progress: CGFloat = 0 { didSet { setNeedsDisplay() } } override func draw(_ rect: CGRect) { let lineWidth: CGFloat = 12 func configure(path: UIBezierPath, color: UIColor) { path.lineWidth = lineWidth path.lineCapStyle = .round color.setStroke() path.stroke() } configure(path: .init( arcCenter: .init(x: frame.width/2, y: frame.width/2), radius: frame.width/2 - lineWidth/2, startAngle: -3*CGFloat.pi/2 + CGFloat.pi/6, endAngle: CGFloat.pi/2 - CGFloat.pi/6, clockwise: true ), color: Asset.marble.color) let progressColor: UIColor = progress >= 0.01 ? Asset.deepWater.color : UIColor.clear configure(path: .init( arcCenter: .init(x: frame.width/2, y: frame.width/2), radius: frame.width/2 - lineWidth/2, startAngle: -3*CGFloat.pi/2 + CGFloat.pi/6, endAngle: CGFloat.pi/2 - CGFloat.pi/6 - (1 - progress)*(2*CGFloat.pi - CGFloat.pi/3), clockwise: true ), color: progressColor) } } class ResourcesViewChartDrawV2: UIView { var progress: CGFloat = 0 { didSet { setNeedsDisplay() } } let lineWidth: CGFloat = 5 override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = .clear } required init?(coder: NSCoder) { super.init(coder: coder) } override func draw(_ rect: CGRect) { // [0,1) Asset.marble.color // [1,5) Asset.brick.color // [5,100] Asset.deepWater.color configure(path: UIBezierPath( arcCenter: .init(x: frame.width/2, y: frame.width/2), radius: frame.width/2 - lineWidth/2, startAngle: -3*CGFloat.pi/2 + CGFloat.pi/6, endAngle: CGFloat.pi/2 - CGFloat.pi/6, clockwise: true ), color: Asset.marble.color) let progressColor: UIColor = progress >= 0.01 ? (progress >= 0.05 ? Asset.deepWater.color : Asset.brick.color) : UIColor.clear configure(path: UIBezierPath( arcCenter: .init(x: frame.width/2, y: frame.width/2), radius: frame.width/2 - lineWidth/2, startAngle: -3*CGFloat.pi/2 + CGFloat.pi/6, endAngle: CGFloat.pi/2 - CGFloat.pi/6 - (1 - progress)*(2*CGFloat.pi - CGFloat.pi/3), clockwise: true ), color: progressColor) } func configure(path: UIBezierPath, color: UIColor) { path.lineWidth = lineWidth path.lineCapStyle = .round color.setStroke() path.stroke() } } class ResourcesViewChart: CommonViewCustom { @IBOutlet private weak var drawView: ResourcesViewChartDraw! @IBOutlet private weak var progressLbl: UILabel! @IBOutlet private weak var titleLbl: UILabel! @IBOutlet private weak var textLbl: UILabel! var progress: CGFloat = 0 { didSet { drawView.progress = progress } } var progressText: String? { didSet { progressLbl.attributedText = progressText?.attributed(style: .medium, size: 24, color: Asset.textGranite.color, alignment: .center) } } var title: String? { didSet { titleLbl.attributedText = title?.attributed(style: .medium, size: 12, color: Asset.textGranite.color, alignment: .center) } } var text: String? { didSet { textLbl.attributedText = text?.attributed(style: .medium, size: 14, color: Asset.textGranite.color, alignment: .center) } } }