Skip to content

Commit

Permalink
Update podspec
Browse files Browse the repository at this point in the history
  • Loading branch information
cruisediary committed May 5, 2017
1 parent becc025 commit 1fcd59d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
8 changes: 3 additions & 5 deletions Pastel.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
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?
# * Try to keep it short, snappy and to the point.
# * 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' => '[email protected]' }
Expand Down
105 changes: 105 additions & 0 deletions Pastel/Classes/PastelView.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Empty file removed Pastel/Classes/ReplaceMe.swift
Empty file.

0 comments on commit 1fcd59d

Please sign in to comment.