Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bibhas/aa4b809623aa38c215b07d7f0c1661ec to your computer and use it in GitHub Desktop.
Save bibhas/aa4b809623aa38c215b07d7f0c1661ec to your computer and use it in GitHub Desktop.
Animate CAGradientLayer
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