|
| 1 | +// |
| 2 | +// PastelView.swift |
| 3 | +// Pastel |
| 4 | +// |
| 5 | +// Created by Cruz on 05/05/2017. |
| 6 | +// Copyright © 2017 CocoaPods. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +class PastelView: UIView { |
| 12 | + |
| 13 | + struct Animation { |
| 14 | + static let keyPath = "colors" |
| 15 | + static let key = "ColorChange" |
| 16 | + } |
| 17 | + |
| 18 | + let gradient = CAGradientLayer() |
| 19 | + var currentGradient: Int = 0 |
| 20 | + open var animationDuration: TimeInterval = 5.0 |
| 21 | + |
| 22 | + private var colors: [UIColor] = [UIColor(red: 156/255, green: 39/255, blue: 176/255, alpha: 1.0), |
| 23 | + UIColor(red: 255/255, green: 64/255, blue: 129/255, alpha: 1.0), |
| 24 | + UIColor(red: 123/255, green: 31/255, blue: 162/255, alpha: 1.0), |
| 25 | + UIColor(red: 32/255, green: 76/255, blue: 255/255, alpha: 1.0), |
| 26 | + UIColor(red: 32/255, green: 158/255, blue: 255/255, alpha: 1.0), |
| 27 | + UIColor(red: 90/255, green: 120/255, blue: 127/255, alpha: 1.0), |
| 28 | + UIColor(red: 58/255, green: 255/255, blue: 217/255, alpha: 1.0)] |
| 29 | + /* |
| 30 | + // Only override draw() if you perform custom drawing. |
| 31 | + // An empty implementation adversely affects performance during animation. |
| 32 | + override func draw(_ rect: CGRect) { |
| 33 | + // Drawing code |
| 34 | + } |
| 35 | + */ |
| 36 | + |
| 37 | + override init(frame: CGRect) { |
| 38 | + super.init(frame: frame) |
| 39 | + } |
| 40 | + |
| 41 | + required init?(coder aDecoder: NSCoder) { |
| 42 | + super.init(coder: aDecoder) |
| 43 | + } |
| 44 | + |
| 45 | + override func awakeFromNib() { |
| 46 | + super.awakeFromNib() |
| 47 | + } |
| 48 | + |
| 49 | + func startAnimation() { |
| 50 | + gradient.removeAllAnimations() |
| 51 | + setup() |
| 52 | + animateGradient() |
| 53 | + } |
| 54 | + |
| 55 | + fileprivate func setup() { |
| 56 | + gradient.frame = bounds |
| 57 | + gradient.colors = currentGradientSet() |
| 58 | + gradient.startPoint = CGPoint(x:0, y:1) |
| 59 | + gradient.endPoint = CGPoint(x:1, y:0) |
| 60 | + gradient.drawsAsynchronously = true |
| 61 | + |
| 62 | + layer.insertSublayer(gradient, at: 0) |
| 63 | + } |
| 64 | + |
| 65 | + fileprivate func currentGradientSet() -> [CGColor] { |
| 66 | + guard colors.count > 0 else { return [] } |
| 67 | + return [colors[currentGradient % colors.count].cgColor, |
| 68 | + colors[(currentGradient + 1) % colors.count].cgColor] |
| 69 | + } |
| 70 | + |
| 71 | + func setColors(colors: [UIColor]) { |
| 72 | + guard colors.count > 0 else { return } |
| 73 | + self.colors = colors |
| 74 | + } |
| 75 | + |
| 76 | + func addColor(color: UIColor) { |
| 77 | + self.colors.append(color) |
| 78 | + } |
| 79 | + |
| 80 | + func animateGradient() { |
| 81 | + currentGradient += 1 |
| 82 | + let animation = CABasicAnimation(keyPath: Animation.keyPath) |
| 83 | + animation.duration = animationDuration |
| 84 | + animation.toValue = currentGradientSet() |
| 85 | + animation.fillMode = kCAFillModeForwards |
| 86 | + animation.isRemovedOnCompletion = false |
| 87 | + animation.delegate = self |
| 88 | + gradient.add(animation, forKey: Animation.key) |
| 89 | + } |
| 90 | + |
| 91 | + override func removeFromSuperview() { |
| 92 | + super.removeFromSuperview() |
| 93 | + gradient.removeAllAnimations() |
| 94 | + gradient.removeFromSuperlayer() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension PastelView: CAAnimationDelegate { |
| 99 | + func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { |
| 100 | + if flag { |
| 101 | + gradient.colors = currentGradientSet() |
| 102 | + animateGradient() |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments