-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
becc025
commit 1fcd59d
Showing
4 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]' } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.