|
| 1 | +// |
| 2 | +// BaseCircleView.swift |
| 3 | +// SwiftyProgressBar |
| 4 | +// |
| 5 | +// Created by Kittikorn Ariyasuk on 2016/12/12. |
| 6 | +// Copyright © 2016 TorIsHere. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +let π:CGFloat = CGFloat(M_PI) |
| 12 | + |
| 13 | +extension CGFloat { |
| 14 | + var doubleValue: Double { return Double(self) } |
| 15 | + var degreesToRadians: CGFloat { return CGFloat(doubleValue * M_PI / 180) } |
| 16 | + var radiansToDegrees: CGFloat { return CGFloat(doubleValue * 180 / M_PI) } |
| 17 | +} |
| 18 | + |
| 19 | +@IBDesignable open class BaseCircleView: UIView { |
| 20 | + let queue = DispatchQueue(label: "com.torishere.progress-queue") |
| 21 | + |
| 22 | + public enum CircleSize:CGFloat { |
| 23 | + case Big = 30 |
| 24 | + case Medium = 15 |
| 25 | + case Small = 8 |
| 26 | + } |
| 27 | + |
| 28 | + @IBInspectable open var circleSize:CircleSize = .Medium |
| 29 | + @IBInspectable open var fgColor: UIColor = UIColor.blue |
| 30 | + @IBInspectable open var bgColor: UIColor = UIColor.gray |
| 31 | + @IBInspectable open var lineWidth:CGFloat = 6 |
| 32 | + @IBInspectable open var bgLineWidth:CGFloat = 5 |
| 33 | + @IBInspectable open var startAngle:CGFloat = -90 |
| 34 | + @IBInspectable open var endAngle:CGFloat = -90 |
| 35 | + @IBInspectable open var willDraw:Bool = true |
| 36 | + |
| 37 | + |
| 38 | + /* |
| 39 | + // Only override draw() if you perform custom drawing. |
| 40 | + // An empty implementation adversely affects performance during animation. |
| 41 | + override func draw(_ rect: CGRect) { |
| 42 | + // Drawing code |
| 43 | + } |
| 44 | + */ |
| 45 | + |
| 46 | + open func drawProgressBar(frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100), progress: CGFloat = 100, fgColor: UIColor = UIColor.blue, bgColor: UIColor = UIColor.gray) { |
| 47 | + |
| 48 | + let center = CGPoint(x:self.bounds.width/2, y: bounds.height/2) |
| 49 | + let radius: CGFloat = max(self.bounds.width, bounds.height) |
| 50 | + |
| 51 | + |
| 52 | + let bgPath:UIBezierPath = UIBezierPath(arcCenter: center, |
| 53 | + radius: radius/2 - lineWidth/2, |
| 54 | + startAngle: -90, |
| 55 | + endAngle: 90, |
| 56 | + clockwise: true) |
| 57 | + bgPath.lineWidth = self.bgLineWidth |
| 58 | + bgColor.setStroke() |
| 59 | + bgPath.stroke() |
| 60 | + |
| 61 | + |
| 62 | + self.endAngle = self.startAngle + (progress * 360) |
| 63 | + let progressPath:UIBezierPath = self.generatePath(startAngle: self.startAngle, endAngle: self.endAngle, size: self.circleSize) |
| 64 | + fgColor.setStroke() |
| 65 | + progressPath.stroke() |
| 66 | + } |
| 67 | + |
| 68 | + open func drawProgressBarWithAnimate(frame: CGRect = CGRect(x: 0, y: 0, width: 100, height: 100), progress: CGFloat = 100, fgColor: UIColor = UIColor.blue, bgColor: UIColor = UIColor.gray, callback: (() -> Void)?) { |
| 69 | + |
| 70 | + let center = CGPoint(x:self.bounds.width/2, y: bounds.height/2) |
| 71 | + let radius: CGFloat = max(self.bounds.width, bounds.height) |
| 72 | + |
| 73 | + |
| 74 | + let bgPath:UIBezierPath = UIBezierPath(arcCenter: center, |
| 75 | + radius: radius/2 - lineWidth/2, |
| 76 | + startAngle: -90, |
| 77 | + endAngle: 90, |
| 78 | + clockwise: true) |
| 79 | + bgPath.lineWidth = self.bgLineWidth |
| 80 | + bgColor.setStroke() |
| 81 | + bgPath.stroke() |
| 82 | + |
| 83 | + self.endAngle = self.startAngle + (progress * 360) |
| 84 | + let progressPath:UIBezierPath = self.generatePath(startAngle: self.startAngle, endAngle: self.endAngle, size: self.circleSize) |
| 85 | + fgColor.setStroke() |
| 86 | + |
| 87 | + let progressLayer = CAShapeLayer() |
| 88 | + progressLayer.path = progressPath.cgPath |
| 89 | + progressLayer.lineCap = kCALineCapRound |
| 90 | + progressLayer.lineWidth = self.lineWidth |
| 91 | + progressLayer.lineJoin = kCALineJoinBevel |
| 92 | + progressLayer.strokeColor = self.fgColor.cgColor |
| 93 | + progressLayer.fillColor = UIColor.clear.cgColor |
| 94 | + self.layer.addSublayer(progressLayer) |
| 95 | + |
| 96 | + |
| 97 | + CATransaction.begin() |
| 98 | + |
| 99 | + CATransaction.setCompletionBlock({ |
| 100 | + if let callback = callback { |
| 101 | + callback() |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + self.progressAnimate(shapeLayer: progressLayer, progress: progress) |
| 106 | + CATransaction.commit() |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + open func hideView() { |
| 111 | + self.isHidden = true |
| 112 | + } |
| 113 | + |
| 114 | + private func progressAnimate(shapeLayer: CAShapeLayer, progress:CGFloat) { |
| 115 | + let animProgress = CABasicAnimation(keyPath: "strokeEnd") |
| 116 | + animProgress.fromValue = 0.0 |
| 117 | + animProgress.toValue = 1.0 |
| 118 | + animProgress.duration = Double(progress) / 1.0 |
| 119 | + animProgress.repeatCount = 0 |
| 120 | + shapeLayer.add(animProgress, forKey: "strokeEnd") |
| 121 | + } |
| 122 | + |
| 123 | + private func generatePath(startAngle:CGFloat, endAngle:CGFloat, size:CircleSize) -> UIBezierPath { |
| 124 | + let center = CGPoint(x:self.bounds.width/2, y: bounds.height/2) |
| 125 | + let radius: CGFloat = max(self.bounds.width, bounds.height) |
| 126 | + |
| 127 | + let path = UIBezierPath(arcCenter: center, |
| 128 | + radius: radius/2 - lineWidth/2, |
| 129 | + startAngle: startAngle.degreesToRadians, |
| 130 | + endAngle: endAngle.degreesToRadians, |
| 131 | + clockwise: true) |
| 132 | + |
| 133 | + switch size { |
| 134 | + case .Big: |
| 135 | + path.lineWidth = CircleSize.Big.rawValue |
| 136 | + case .Medium: |
| 137 | + path.lineWidth = CircleSize.Medium.rawValue |
| 138 | + case .Small: |
| 139 | + path.lineWidth = CircleSize.Small.rawValue |
| 140 | + default: |
| 141 | + path.lineWidth = CircleSize.Medium.rawValue |
| 142 | + } |
| 143 | + |
| 144 | + path.lineCapStyle = CGLineCap.round |
| 145 | + |
| 146 | + return path |
| 147 | + } |
| 148 | + |
| 149 | + override public init (frame : CGRect) { |
| 150 | + super.init(frame : frame) |
| 151 | + } |
| 152 | + |
| 153 | + convenience public init () { |
| 154 | + self.init(frame:CGRect.zero) |
| 155 | + } |
| 156 | + |
| 157 | + required public init?(coder aDecoder: NSCoder) { |
| 158 | + //fatalError("init(coder:) has not been implemented") |
| 159 | + super.init(coder: aDecoder) |
| 160 | + } |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +struct Future<T> { |
| 165 | + typealias FutureResultHandler = (T) -> () |
| 166 | + typealias AsyncOperation = (@escaping FutureResultHandler) -> () |
| 167 | + |
| 168 | + private let asyncOperation: AsyncOperation |
| 169 | + |
| 170 | + init(asyncOperation: @escaping AsyncOperation) { |
| 171 | + self.asyncOperation = asyncOperation |
| 172 | + } |
| 173 | + |
| 174 | + func resolve(_ handler: @escaping FutureResultHandler) { |
| 175 | + asyncOperation(handler) |
| 176 | + } |
| 177 | + |
| 178 | + func then<U>(_ next: @escaping (_ input: T, _ callback: @escaping (U) -> ()) -> ()) -> Future<U> { |
| 179 | + return Future<U> { (resultHandler) in |
| 180 | + self.resolve { firstResult in |
| 181 | + next(firstResult) { secondResult in |
| 182 | + resultHandler(secondResult) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
0 commit comments