-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
91 lines (59 loc) · 1.81 KB
/
ViewController.swift
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//grad1()
// grad2()
grad3()
}
//Example 1
func grad1(){
//in iOS gradient is a sort of layer
let layer = CAGradientLayer()
//gradient colors
layer.colors = [
UIColor.red.cgColor, //start color
UIColor.blue.cgColor //end color
]
//resuse CGRect of View
layer.frame = view.frame
layer.frame.origin = CGPoint(x: 0, y: 0)
//add gradient layer to existing list of layers of that view
view.layer.insertSublayer(layer, at: 0)
}
//Example 2
func grad2(){
let l = CAGradientLayer()
l.colors = [
UIColor.red.cgColor,
UIColor.blue.cgColor,
UIColor.red.cgColor,
]
//peak points
l.locations = [0.1, 0.3, 0.8]
let s = view.frame.size
l.frame = CGRect(
origin: CGPoint(x:0, y:0),
size: CGSize(width: s.width, height: s.height))
view.layer.addSublayer(l)
}
func grad3(){
let l = CAGradientLayer()
l.colors = [UIColor.red.cgColor,
UIColor.cyan.cgColor]
//directions
l.startPoint = CGPoint(x: 0, y: 0)
l.endPoint = CGPoint(x: 1, y: 1)
l.frame.size = view.frame.size
l.frame.origin.x = 0
l.frame.origin.y = 0
/*
//center in view
let s = view.frame.size
l.frame.size = CGSize(width: s.width / 2, height: s.height / 2)
l.frame.origin.x = s.width / 4
l.frame.origin.y = s.height / 4
*/
view.layer.addSublayer(l)
}
}