Created
April 21, 2021 13:20
-
-
Save artturijalli/4a1cdf3e83ff90af18ec5c578e677405 to your computer and use it in GitHub Desktop.
Add a smooth gradient to your app by pasting this view controller's contents to the view's controller you want to animate
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
import UIKit | |
class ViewController: UIViewController, CAAnimationDelegate { | |
let color1: CGColor = UIColor(red: 209/255, green: 107/255, blue: 165/255, alpha: 1).cgColor | |
let color2: CGColor = UIColor(red: 134/255, green: 168/255, blue: 231/255, alpha: 1).cgColor | |
let color3: CGColor = UIColor(red: 95/255, green: 251/255, blue: 241/255, alpha: 1).cgColor | |
let gradient: CAGradientLayer = CAGradientLayer() | |
var gradientColorSet: [[CGColor]] = [] | |
var colorIndex: Int = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
setupGradient() | |
animateGradient() | |
} | |
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { | |
if flag { | |
animateGradient() | |
} | |
} | |
func setupGradient(){ | |
gradientColorSet = [ | |
[color1, color2], | |
[color2, color3], | |
[color3, color1] | |
] | |
gradient.frame = self.view.bounds | |
gradient.colors = gradientColorSet[colorIndex] | |
self.view.layer.addSublayer(gradient) | |
} | |
func animateGradient() { | |
gradient.colors = gradientColorSet[colorIndex] | |
let gradientAnimation = CABasicAnimation(keyPath: "colors") | |
gradientAnimation.delegate = self | |
gradientAnimation.duration = 3.0 | |
updateColorIndex() | |
gradientAnimation.toValue = gradientColorSet[colorIndex] | |
gradientAnimation.fillMode = .forwards | |
gradientAnimation.isRemovedOnCompletion = false | |
gradient.add(gradientAnimation, forKey: "colors") | |
} | |
func updateColorIndex(){ | |
if colorIndex < gradientColorSet.count - 1 { | |
colorIndex += 1 | |
} else { | |
colorIndex = 0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment