diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..9f55b2c --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +3.0 diff --git a/Pastel.podspec b/Pastel.podspec index 0bd41ee..28ffaa4 100644 --- a/Pastel.podspec +++ b/Pastel.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = 'Pastel' s.version = '0.1.0' - s.summary = 'A short description of Pastel.' + s.summary = 'Instagram like gradient background animation' # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? @@ -17,11 +17,9 @@ Pod::Spec.new do |s| # * Write the description between the DESC delimiters below. # * Finally, don't worry about the indent, CocoaPods strips it! - s.description = <<-DESC -TODO: Add long description of the pod here. - DESC + s.description = 'Instagram like infinity gradient background animation' - s.homepage = 'https://github.com/cruz/Pastel' + s.homepage = 'https://github.com/cruisediary/Pastel' # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'cruz' => 'cruzdiary@gmail.com' } diff --git a/Pastel/Classes/PastelView.swift b/Pastel/Classes/PastelView.swift new file mode 100644 index 0000000..db98f4c --- /dev/null +++ b/Pastel/Classes/PastelView.swift @@ -0,0 +1,105 @@ +// +// PastelView.swift +// Pastel +// +// Created by Cruz on 05/05/2017. +// Copyright © 2017 CocoaPods. All rights reserved. +// + +import UIKit + +class PastelView: UIView { + + struct Animation { + static let keyPath = "colors" + static let key = "ColorChange" + } + + let gradient = CAGradientLayer() + var currentGradient: Int = 0 + open var animationDuration: TimeInterval = 5.0 + + private var colors: [UIColor] = [UIColor(red: 156/255, green: 39/255, blue: 176/255, alpha: 1.0), + UIColor(red: 255/255, green: 64/255, blue: 129/255, alpha: 1.0), + UIColor(red: 123/255, green: 31/255, blue: 162/255, alpha: 1.0), + UIColor(red: 32/255, green: 76/255, blue: 255/255, alpha: 1.0), + UIColor(red: 32/255, green: 158/255, blue: 255/255, alpha: 1.0), + UIColor(red: 90/255, green: 120/255, blue: 127/255, alpha: 1.0), + UIColor(red: 58/255, green: 255/255, blue: 217/255, alpha: 1.0)] + /* + // Only override draw() if you perform custom drawing. + // An empty implementation adversely affects performance during animation. + override func draw(_ rect: CGRect) { + // Drawing code + } + */ + + override init(frame: CGRect) { + super.init(frame: frame) + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + } + + override func awakeFromNib() { + super.awakeFromNib() + } + + func startAnimation() { + gradient.removeAllAnimations() + setup() + animateGradient() + } + + fileprivate func setup() { + gradient.frame = bounds + gradient.colors = currentGradientSet() + gradient.startPoint = CGPoint(x:0, y:1) + gradient.endPoint = CGPoint(x:1, y:0) + gradient.drawsAsynchronously = true + + layer.insertSublayer(gradient, at: 0) + } + + fileprivate func currentGradientSet() -> [CGColor] { + guard colors.count > 0 else { return [] } + return [colors[currentGradient % colors.count].cgColor, + colors[(currentGradient + 1) % colors.count].cgColor] + } + + func setColors(colors: [UIColor]) { + guard colors.count > 0 else { return } + self.colors = colors + } + + func addColor(color: UIColor) { + self.colors.append(color) + } + + func animateGradient() { + currentGradient += 1 + let animation = CABasicAnimation(keyPath: Animation.keyPath) + animation.duration = animationDuration + animation.toValue = currentGradientSet() + animation.fillMode = kCAFillModeForwards + animation.isRemovedOnCompletion = false + animation.delegate = self + gradient.add(animation, forKey: Animation.key) + } + + override func removeFromSuperview() { + super.removeFromSuperview() + gradient.removeAllAnimations() + gradient.removeFromSuperlayer() + } +} + +extension PastelView: CAAnimationDelegate { + func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { + if flag { + gradient.colors = currentGradientSet() + animateGradient() + } + } +} diff --git a/Pastel/Classes/ReplaceMe.swift b/Pastel/Classes/ReplaceMe.swift deleted file mode 100644 index e69de29..0000000