123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import UIKit
- @IBDesignable
- public class DSGradientProgressView: UIView, CAAnimationDelegate {
-
- @IBInspectable public var barColor: UIColor = UIColor(hue: (29.0/360.0), saturation: 1.0, brightness: 1.0, alpha: 1.0) {
- didSet {
- initialize()
- }
- }
-
- private let serialIncrementQueue = DispatchQueue(label: "com.dholstudio.DSGradientProgressView.serialIncrementQueue")
-
- private var numberOfOperations: Int = 0
-
- override public class var layerClass: AnyClass {
- get {
- return CAGradientLayer.self
- }
- }
-
-
-
- override public init(frame: CGRect) {
- super.init(frame: frame)
-
- self.initialize()
- }
-
- required public init(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)!
-
- self.initialize()
- }
-
- override public func awakeFromNib() {
- super.awakeFromNib()
-
- self.initialize()
- }
-
- private func initialize() {
-
- let layer = self.layer as! CAGradientLayer
-
-
- layer.startPoint = CGPoint(x: 0.0, y: 0.5)
- layer.endPoint = CGPoint(x: 1.0, y: 0.5)
-
- var colors: [CGColor] = []
-
-
-
-
-
-
-
-
-
-
-
-
- for alpha in stride(from: 0, through: 40, by: 2) {
-
- let color = barColor.withAlphaComponent(CGFloat(Double(alpha)/100.0))
-
- colors.append(color.cgColor)
- }
-
- for alpha in stride(from: 40, through: 90, by: 10) {
-
- let color = barColor.withAlphaComponent(CGFloat(Double(alpha)/100.0))
-
- colors.append(color.cgColor)
- }
-
- for alpha in stride(from: 90, through: 100, by: 10) {
-
- let color = barColor.withAlphaComponent(CGFloat(Double(alpha)/100.0))
-
- colors.append(color.cgColor)
- colors.append(color.cgColor)
- }
-
- for alpha in stride(from: 100, through: 0, by: -20) {
-
- let color = barColor.withAlphaComponent(CGFloat(Double(alpha)/100.0))
-
- colors.append(color.cgColor)
- }
-
- layer.colors = colors
- }
-
- private func performAnimation() {
-
-
-
- let layer = self.layer as! CAGradientLayer
-
- guard let color = layer.colors?.popLast() else {
- print("FATAL ERR: GradientProgressView : Layer should contain colors!")
- return
- }
-
- layer.colors?.insert(color, at: 0)
-
- let shiftedColors = layer.colors!
-
- let animation = CABasicAnimation(keyPath: "colors")
- animation.toValue = shiftedColors
- animation.duration = 0.03
- animation.isRemovedOnCompletion = true
- animation.fillMode = CAMediaTimingFillMode.forwards
- animation.delegate = self
- layer.add(animation, forKey: "animateGradient")
- }
-
- public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
-
-
-
- if flag && numberOfOperations > 0 {
-
- performAnimation()
- }
- else {
- self.isHidden = true
- }
- }
-
- public func startProgress() {
- serialIncrementQueue.sync {
- numberOfOperations += 1
- }
- self.isHidden = false
-
- if numberOfOperations == 1 {
- performAnimation()
- }
- }
-
- public func stopProgress() {
-
- if numberOfOperations == 0 {
- return
- }
-
- serialIncrementQueue.sync {
- numberOfOperations -= 1
- }
- }
-
-
-
- }
|