I had the opportunity to draw a gradient in UIView using CAGradientLayer I briefly summarized the mounting method.
startPoint ・ ・ ・ Start point of gradation endPoint ・ ・ ・ End point of gradation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setGradation()
}
private func setGradation() {
let gradientLayer = CAGradientLayer()
//Specify the range to be gradation to the view size of UIViewController
gradientLayer.frame.size = self.view.frame.size
gradientLayer.colors = [UIColor.green.cgColor, UIColor.white.cgColor]
self.view.layer.addSublayer(gradientLayer)
}
}
The default values of startPoint and endPoint of the gradation are as follows (gradient from top center to bottom center)
startPoint | endPoint |
---|---|
CGPoint(x: 0.5, y: 0.0) | CGPoint(x: 0.5, y: 1.0) |
It is drawn as follows.
As mentioned above, the values of startPoint and endPoint of the gradation are set by default, but you can also specify them.
In the code below
startPoint: CGPoint (x: 0.0, y: 1.0)
(lower left)
endPoint: CGPoint (x: 1.0, y: 0.0)
(upper right)
Therefore, it will be drawn as shown in the capture below.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setGradation()
}
private func setGradation() {
let gradientLayer = CAGradientLayer()
//Specify the range to be gradation to the view size of UIViewController
gradientLayer.frame.size = self.view.frame.size
gradientLayer.colors = [UIColor.blue.cgColor, UIColor.white.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
self.view.layer.addSublayer(gradientLayer)
}
}
Recommended Posts