--Create a class for gradation --Load class for gradient --Pass the value to the class for gradient
This time I will make another class. If you want to change the color depending on the page, this one is more effective for crushing, and ** readability ** is also improved (not scolded by the reviewer).
ChangeColor.swift
import Foundation
import UIKit
class ChangeColor {
func changeColor(topR:CGFloat, topG:CGFloat, topB:CGFloat,
topAlpha:CGFloat,
bottomR:CGFloat, bottomG:CGFloat, bottomB:CGFloat,
bottomAlpha:CGFloat)
->CAGradientLayer {
//Gradient start color
let topColor = UIColor(red: topR, green: topG, blue: topB, alpha: topAlpha)
//Gradient end color
let bottomColor = UIColor(red: bottomR, green: bottomG, blue: bottomB, alpha: bottomAlpha)
//Manage gradation colors in an array
let gradientColor = [topColor.cgColor, bottomColor.cgColor]
let gradientLayer = CAGradientLayer()
gradientLayer.colors = gradientColor
return gradientLayer
}
}
ViewController.swift
class ViewController: UIViewController {
//Instantiate a color setting class
var changeColor = ChangeColor()
var gradientLayer = CAGradientLayer()
override func viewDidLoad() {
super.viewDidLoad()
//Pass a value to the color settings class
gradientLayer = changeColor.changeColor(topR: 0.07, topG: 0.13, topB: 0.26, topAlpha: 1.0, bottomR: 0.54, bottomG: 0.74, bottomB: 0.74, bottomAlpha: 1.0)
//bounds represent the whole
gradientLayer.frame = view.bounds
view.layer.insertSublayer(gradientLayer, at: 0)
}
}
By the way, what is "instantiating"? I am using it vaguely.