Skip to content

Instantly share code, notes, and snippets.

@dakrawczyk
Created August 23, 2016 20:59
Show Gist options
  • Save dakrawczyk/396c650009cdbaa3eceb8028688ed1b4 to your computer and use it in GitHub Desktop.
Save dakrawczyk/396c650009cdbaa3eceb8028688ed1b4 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// gradientView
//
// Created by Dave Krawczyk on 8/23/16.
// Copyright © 2016 Windy City Lab. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var bottomView: UIView!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.bottomView.addGradientAnimation()
self.button.addGradientAnimation()
}
}
extension UIColor {
// Green: 128  205  201
// Purple:  161  115  204
class func glanceGreen() -> UIColor {
return UIColor(red: (128.0/255.0), green: (205.0/255.0), blue: (201.0/255.0), alpha: 1)
}
class func glancePurple() -> UIColor {
return UIColor(red: (161.0/255.0), green: (115.0/255.0), blue: (204.0/255.0), alpha: 1)
}
}
extension UIView {
func addGradientAnimation() {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [UIColor.glanceGreen().cgColor, UIColor.glancePurple().cgColor]
gradient.startPoint = CGPoint(x: 1, y: 0)
gradient.endPoint = CGPoint(x: 0, y: 1)
self.layer.insertSublayer(gradient, at: 0)
let fromColors = gradient.colors
let toColors = [UIColor.glancePurple().cgColor, UIColor.glanceGreen().cgColor]
gradient.colors = toColors
let animation = CABasicAnimation(keyPath: "colors")
animation.fromValue = fromColors
animation.toValue = toColors
animation.duration = 10.00
animation.isRemovedOnCompletion = true
animation.fillMode = kCAFillModeForwards
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.autoreverses = true
animation.repeatCount = Float.infinity
gradient.add(animation, forKey: "animateGradient")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment