Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Created March 30, 2019 10:54
Show Gist options
  • Save Bilguun132/9e689159eae249da21d062ea736baf17 to your computer and use it in GitHub Desktop.
Save Bilguun132/9e689159eae249da21d062ea736baf17 to your computer and use it in GitHub Desktop.
CoreAnimationTutorial-ViewController.swift
//
// ViewController.swift
// Core Animations
//
// Created by Bilguun Batbold on 29/3/19.
// Copyright © 2019 Bilguun. All rights reserved.
//
import UIKit
class ViewController: UIViewController, CAAnimationDelegate {
let gradient = CAGradientLayer()
// list of array holding 2 colors
var gradientSet = [[CGColor]]()
// current gradient index
var currentGradient: Int = 0
// colors to be added to the set
let colorOne = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1).cgColor
let colorTwo = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1).cgColor
let colorThree = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1).cgColor
// create outlet in the storyboard with type CountdownProgressBar
@IBOutlet weak var countdownProgressBar: CountdownProgressBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
createGradientView()
countdownProgressBar.startCoundown(duration: 10, showPulse: true)
}
/// Creates gradient view
func createGradientView() {
// overlap the colors and make it 3 sets of colors
gradientSet.append([colorOne, colorTwo])
gradientSet.append([colorTwo, colorThree])
gradientSet.append([colorThree, colorOne])
// set the gradient size to be the entire screen
gradient.frame = self.view.bounds
gradient.colors = gradientSet[currentGradient]
gradient.startPoint = CGPoint(x:0, y:0)
gradient.endPoint = CGPoint(x:1, y:1)
gradient.drawsAsynchronously = true
self.view.layer.insertSublayer(gradient, at: 0)
animateGradient()
}
@objc func handleTap() {
print("Tapped")
countdownProgressBar.startCoundown(duration: 10, showPulse: true)
}
func animateGradient() {
// cycle through all the colors, feel free to add more to the set
if currentGradient < gradientSet.count - 1 {
currentGradient += 1
} else {
currentGradient = 0
}
// animate over 3 seconds
let gradientChangeAnimation = CABasicAnimation(keyPath: "colors")
gradientChangeAnimation.duration = 3.0
gradientChangeAnimation.toValue = gradientSet[currentGradient]
gradientChangeAnimation.fillMode = CAMediaTimingFillMode.forwards
gradientChangeAnimation.isRemovedOnCompletion = false
gradientChangeAnimation.delegate = self
gradient.add(gradientChangeAnimation, forKey: "gradientChangeAnimation")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
// if our gradient animation ended animating, restart the animation by changing the color set
if flag {
gradient.colors = gradientSet[currentGradient]
animateGradient()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment