Forked from everlof/gist:59b1f5dd8fa569effb08bedec35e69b4
Created
August 29, 2018 16:45
-
-
Save bibhas/aa4b809623aa38c215b07d7f0c1661ec to your computer and use it in GitHub Desktop.
Animate CAGradientLayer
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
let startLocations: [NSNumber] = [ 0.0, 0.05, 0.25, 0.3] | |
let endLocations: [NSNumber] = [ 0.7, 0.75, 0.95, 1] | |
gradient.startPoint = CGPoint(x: -0, y: 0.5) | |
gradient.endPoint = CGPoint(x: 1, y: 0.5) | |
gradient.colors = [ UIColor.white.cgColor, UIColor.tetraDarkBlue.cgColor, UIColor.tetraDarkBlue.cgColor, UIColor.white.cgColor ] | |
gradient.locations = startLocations | |
layer.insertSublayer(gradient, at: 0) | |
let animation = CABasicAnimation(keyPath: "locations") // CABasicAnimation(keyPath: "colors") | |
animation.fromValue = startLocations // [ UIColor.red.cgColor, UIColor.blue.cgColor ] // [ 0.9 ] | |
animation.toValue = endLocations // [ UIColor.blue.cgColor, UIColor.red.cgColor ] // [ 0.2 ] | |
animation.duration = 1.5 | |
animation.autoreverses = true | |
animation.repeatCount = 9999 | |
animation.delegate = self | |
gradient.add(animation, forKey: "locations") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment