Skip to content

Commit 1fcd59d

Browse files
committedMay 5, 2017
Update podspec
1 parent becc025 commit 1fcd59d

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed
 

‎.swift-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.0

‎Pastel.podspec

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
Pod::Spec.new do |s|
1010
s.name = 'Pastel'
1111
s.version = '0.1.0'
12-
s.summary = 'A short description of Pastel.'
12+
s.summary = 'Instagram like gradient background animation'
1313

1414
# This description is used to generate tags and improve search results.
1515
# * Think: What does it do? Why did you write it? What is the focus?
1616
# * Try to keep it short, snappy and to the point.
1717
# * Write the description between the DESC delimiters below.
1818
# * Finally, don't worry about the indent, CocoaPods strips it!
1919

20-
s.description = <<-DESC
21-
TODO: Add long description of the pod here.
22-
DESC
20+
s.description = 'Instagram like infinity gradient background animation'
2321

24-
s.homepage = 'https://github.com/cruz/Pastel'
22+
s.homepage = 'https://github.com/cruisediary/Pastel'
2523
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
2624
s.license = { :type => 'MIT', :file => 'LICENSE' }
2725
s.author = { 'cruz' => 'cruzdiary@gmail.com' }

‎Pastel/Classes/PastelView.swift

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

‎Pastel/Classes/ReplaceMe.swift

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.