1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupLayer()
}
func setupLayer() {
//创建出layer
let gradientLayer = CAGradientLayer()
gradientLayer.frame = view.frame
//设置颜色数组
gradientLayer.colors = [cgColorFor(red: 200, green: 0, blue: 0),
cgColorFor(red: 255, green: 100, blue: 30),
cgColorFor(red: 255, green: 200, blue: 30),
cgColorFor(red: 50, green: 200, blue: 0),
cgColorFor(red: 30, green: 0, blue: 100),
cgColorFor(red: 50, green: 0, blue: 70)]
//设置锚点
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 0, y: 1)
view.layer.addSublayer(gradientLayer)
}
func cgColorFor(red: CGFloat, green: CGFloat, blue: CGFloat) -> CGColor {
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: 1).cgColor
}
}
|